using timeoutsocket.py

Garth Grimm garth_grimm at hp.com
Wed Oct 10 13:02:36 EDT 2001


Thanks to Oleg who pointed me to http://www.timo-tasi.org/python/timeoutsocket.py as a solution to 
my problem.  Unfortunately, I'm getting some unusual behavior.

The situation is that while handling an HTTP request from one user, I'm sending and receiving some 
data from another server, which I then repurpose in formulating the response to the user's request. 
  The code below works, but I'm trying to protect against the failure mode in which the second 
server never responds by timing out the connection.


import httplib              # to generate the HTTP requests
#import timeoutsocket        # to add timeout characteristics to all socket connections

# set the timeout to 15 seconds
#timeoutsocket.setDefaultSocketTimeout(15)

#  declare and initialize some variables
patchDisplay = [] 	# this is the list of 3-element tuples that we want to end up with
patchQS = []        	# repository to store the potential patch names
REQUEST_DOMAIN = 'some.domain.com'          # the domain to sent the request to
REQUEST_PATH = '/URLpath/file.html?PATCHIDS='  # the URL Path to send the request to

# Code to construct patchQS is here

#  Construct the HTTP request to retrieve the XML file describing exact matches for the list of
#  matches
try:
     h = httplib.HTTP(REQUEST_DOMAIN)
     h.putrequest('GET', REQUEST_PATH + string.join(patchQS, ',') )
     h.putheader('Accept', 'text/xml')
     h.endheaders()
     errcode, errmsg, headers = h.getreply()
# Protect against all kinds of connection errors, by catching exceptions and designating errcode to 404.
except: errcode = 404

#  Send the request and parse the XML data if a valid response is returned.
if errcode == 200:
     try:
#        Parsing routine to build patchDisplay is here
#        this includes a use of h.getfile().read()
     except Exception:
         pass
         patchDisplay = []

When I uncomment out the timeoutsocket lines to add the protection, the first request is processed 
fine, but subsequent requsts generate an exception that has the following trace:

socket.error: (10056, 'Socket is already connected')
   File <string>, line 1, in connect
     args = (('some.domain.com', 80),)
     self = <socket._socketobject instance at 05E83E3C>
   File timeoutsocket.py, line 252, in connect
     addr = ('some.domain.com', 80)
     blocking = 1
     dumbhack = 1
     errcode = 10056
     port = None
     self = <timeoutsocket.TimeoutSocket instance at 05E83E64>
     sock = <socket._socketobject instance at 05E83E3C>
     timeout = None
     why = <socket.error instance at 05E83F54>
   File timeoutsocket.py, line 261, in connect
     addr = ('some.domain.com', 80)
     blocking = 1
     dumbhack = None
     e = []
     errcode = 10035
     port = None
     r = []
     self = <timeoutsocket.TimeoutSocket instance at 05E83E64>
     sock = <socket._socketobject instance at 05E83E3C>
     timeout = None
     w = [<socket._socketobject instance at 05E83E3C>]
     why = <socket.error instance at 05E83EDC>
   File httplib.py, line 351, in connect
     self = <httplib.HTTPConnection instance at 05E83E14>
   File httplib.py, line 367, in send
     self = <httplib.HTTPConnection instance at 05E83E14>
     str = 'GET /URLpath/file.html?PATCHIDS=PHSS_24591
   File httplib.py, line 429, in putrequest
     method = 'GET'
     self = <httplib.HTTPConnection instance at 05E83E14>
     str = 'GET /URLpath/file.html?PATCHIDS=PHSS_24591
     url = '/URLpath/file.html?PATCHIDS=PHSS_24591'
     v = <socket.error instance at 05E83F54>

Is the timeoutsocket.py wrapper around the base socket class preventing connections from being 
closed?  If so, any ideas on how to fix it?  I've attempted to do a h.close() at the end of the XML 
parsing routine, but that didn't solve the problem.

TIA,
Garth




More information about the Python-list mailing list