I have code to read a url like:
from urllib2 import Request, urlopen req = Request(url) for key, val in headers.items(): req.add_header(key, val) res = urlopen(req, timeout = timeout)
A timeout works for calling urlopen (). But then the code gets to the res.read () call, where I want to read the response data, and the timeout is not applied there. Thus, a read call can hang almost forever, waiting for data from the server. The only solution I found was to use a signal to interrupt read (), which is not suitable for me, since I use streams.
What other options are there? Is there an HTTP library for Python that handles read timeouts? I looked at httplib2 and the requests and they seem to suffer from the same problem as above. I donโt want to write my own non-blocking network code using the socket module, because I think there should already be a library for this.
Update: None of the solutions below do this for me. You yourself can make sure that setting a timeout for a socket or urlopen does not affect the loading of a large file:
from urllib2 import urlopen url = 'http://iso.linuxquestions.org/download/388/7163/http/se.releases.ubuntu.com/ubuntu-12.04.3-desktop-i386.iso' c = urlopen(url) c.read()
At least on Windows with Python 2.7.3, timeouts are completely ignored.
Bjรถrn Lindqvist Mar 03 '12 at 18:51 2012-03-03 18:51
source share