Get socket for urllib2.urlopen return value for HTTP

I am trying to asynchronously upload files using urllib2, but could not find the socket (or its fileno) to wait for new data for HTTP requests. Here is what I have already tried.

>>> 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'
>>> 
+5
source share
1 answer

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

The problem is that urlib2 has been modified to wrap the HTTPResponse object in socket._fileobject to get some more file methods. Except (as above), HTTPResponse does not have a fileno () method, so when _fileobject tries to use it, it explodes.

Decision

HTTPResponse:

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

, , urllib.urlopen urrlib2.urlopen.

; Python 3 Python 2.7.

+2

All Articles