我試圖輪詢urllib2.urlopen()返回的類文件對象時遇到http://bugs.python.org/issue1327971中描述的錯誤。對由urllib2.urlopen()返回的類文件對象使用poll?
不幸的是,對於Python相對來說比較陌生,我實際上無法從響應中確定如何解決這個問題,因爲他們似乎主要致力於修復該錯誤,而不是篡改觸發其工作的代碼。
這裏是我的代碼蒸餾版本引發錯誤:
import urllib2, select
if __name__ == "__main__":
p = select.poll()
url = "http://localhost/"
fd = urllib2.urlopen(url)
p.register(fd, select.POLLIN | select.POLLERR | select.POLLHUP | select.POLLNVAL)
result = p.poll()
for fd, event in result:
if event == select.POLLIN:
while 1:
buf = fd.read(4096)
if not buf:
break
print buf
當我在Python 2.6中運行它,它引發的錯誤:
Traceback (most recent call last):
File "/home/shab/py/test.py", line 9, in <module>
p.register(fd, select.POLLIN | select.POLLERR | select.POLLHUP | select.POLLNVAL)
File "/usr/lib/python2.6/socket.py", line 287, in fileno
return self._sock.fileno()
AttributeError: HTTPResponse instance has no attribute 'fileno'
更新:我不知道想修改系統庫。