gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:30:17 30 4
gpt4 key购买 nike

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

>>> 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'
>>>

最佳答案

参见 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 返回值的套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6990340/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com