python – 为urllib2.urlopen获取HTTP的返回值

我正在尝试使用urllib2进行文件的异步下载,但没有成功找到套接字(或其fileno)以等待HTTP请求的新数据.这是我已经尝试过的.

>>> from urllib2 import urlopen
>>> from select import select
>>> r = urlopen('https://stackoverflow.com/')
>>> select([r], [], [])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/socket.py", line 307, in fileno
    return self._sock.fileno()
AttributeError: HTTPResponse instance has no attribute 'fileno'
>>> r.fileno()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/socket.py", line 307, in fileno
    return self._sock.fileno()
AttributeError: HTTPResponse instance has no attribute 'fileno'
>>> r.fp.fileno()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/socket.py", line 307, in fileno
    return self._sock.fileno()
AttributeError: HTTPResponse instance has no attribute 'fileno'
>>> select([r.fp], [], [])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/socket.py", line 307, in fileno
    return self._sock.fileno()
AttributeError: HTTPResponse instance has no attribute 'fileno'
>>> 
最佳答案
http://www.velocityreviews.com/forums/t512553-re-urllib2-urlopen-broken.html.

The problem is that urlib2 was changed to wrap an HTTPResponse object
in a socket._fileobject to get a few more file methods. Except (as
reported above) HTTPResponse doesn’t have a fileno() method, so when
_fileobject tries to use it, it blows up.

解决方案

向HTTPResponse添加适当的方法:

def fileno(self):
    return self.fp.fileno()

或者,使用urllib.urlopen而不是urrlib2.urlopen.

这个问题有一个bug report;它在Python 3和Python 2.7中得到修复.

转载注明原文:python – 为urllib2.urlopen获取HTTP的返回值 - 代码日志