Non-blocking read() in httplib?

Benjamin Niemann b.niemann at betternet.de
Mon Jul 12 14:31:19 CEST 2004


Marcin Ciura wrote:
> I have the following problem with HTTPResponse:
> 
> import httplib #, select
> ...
>     connection = httplib.HTTPConnection(host)
>     connection.connect()
>     connection.request('GET', url)
>     response = connection.getresponse()
>     # print response.status, response.reason gives '200 OK'
>     # ready = select.select([response],[],[], 5.0) # no fileno() method
>     # signal.alarm(5) # not on Windows
>     # ...
>     data=response.read() # this sometimes blocks
>     connection.close()
> 
> Sometimes the read() call blocks forever for no obvious reason 
> (response.status is OK); it even cannot be interrupted from the keyboard 
> (on Windows).
> 
> I would like to defend against this by throwing an exception when the 
> read() lasts too long. But I cannot use select.select(), because 
> HTTPResponse has no fileno() method. Neither can I use signal.alarm(), 
> as it is for Unixes only.
> 
> Is there any other way to break read() or make it non-blocking?
I'm using timeoutsocket.py which allows you to globally specify a 
timeout for all sockets. Used signal.alarm before, which caused more 
problems than it solved...



More information about the Python-list mailing list