Read timeout using either urllib2 or any other http library

后端 未结 8 965
花落未央
花落未央 2020-11-29 05:36

I have code for reading an url like this:

from urllib2 import Request, urlopen
req = Request(url)
for key, val in headers.items():
    req.add_header(key, va         


        
8条回答
  •  难免孤独
    2020-11-29 05:46

    I found in my tests (using the technique described here) that a timeout set in the urlopen() call also effects the read() call:

    import urllib2 as u
    c = u.urlopen('http://localhost/', timeout=5.0)
    s = c.read(1<<20)
    Traceback (most recent call last):
      File "", line 1, in 
      File "/usr/lib/python2.7/socket.py", line 380, in read
        data = self._sock.recv(left)
      File "/usr/lib/python2.7/httplib.py", line 561, in read
        s = self.fp.read(amt)
      File "/usr/lib/python2.7/httplib.py", line 1298, in read
        return s + self._file.read(amt - len(s))
      File "/usr/lib/python2.7/socket.py", line 380, in read
        data = self._sock.recv(left)
    socket.timeout: timed out
    

    Maybe it's a feature of newer versions? I'm using Python 2.7 on a 12.04 Ubuntu straight out of the box.

提交回复
热议问题