获取HTTP的urllib2.urlopen返回值的套接字

时间:2011-08-09 01:05:29

标签: python urllib2 urllib

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

>>> from urllib2 import urlopen
>>> from select import select
>>> r = urlopen('http://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'
>>> 

1 个答案:

答案 0 :(得分:2)

请参阅http://www.velocityreviews.com/forums/t512553-re-urllib2-urlopen-broken.html

  

问题是urlib2已更改为包装HTTPResponse对象   在socket._fileobject中获取更多文件方法。除外(如   上面报道的)HTTPResponse没有fileno()方法,所以当   _fileobject尝试使用它,它会爆炸。

解决方案

为HTTPResponse添加适当的方法:

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

或者,使用urllib.urlopen代替urrlib2.urlopen

这个问题有一个bug report;它已在Python 3和Python 2.7中修复。