HTTP

2011-08-09 18 views
5

için urllib2.urlopen dönüş değeri için yuva alın Urllib2'yi kullanarak dosyaların eşzamansız indirilmesini deniyorum, ancak HTTP istekleri için yeni verileri beklemek üzere soketi (veya onun fileno'sunu) bulmakta başarılı olamadım. İşte şimdiden denedim.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' 
>>> 

cevap

2

Bkz. http://www.velocityreviews.com/forums/t512553-re-urllib2-urlopen-broken.html.

sorun urlib2 birkaç dosya yöntemlerini almak için bir socket._fileobject bir HTTPResponse nesnesi sarmak için değiştirildi olmasıdır. Hariç (yukarıdaki gibi ) HTTPResponse bir fileno() yöntemine sahip değildir, bu nedenle _fileobject kullanmayı denediğinde, püskürtür.

HTTPResponse için uygun bir ekleme yöntemi çözelti

:

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

Veya alternatif olarak, urllib.urlopen yerine urrlib2.urlopen kullanın.

Bu sorun için bir bug report var; Python 3 ve Python 2.7'de sabitlenmiştir.

+0

Teşekkürler, bayım! –

+0

Zaten bir tane yoksa, bir hata yükseltilmelidir. –

+0

http://bugs.python.org/issue1327971. Aslında bunun için düzeltmeyi yapan kişi siz misiniz? – agf

İlgili konular