masperro / httplib2

Automatically exported from code.google.com/p/httplib2
0 stars 0 forks source link

httplib with proxy: AttributeError: 'NoneType' object has no attribute 'makefile' #101

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
I did what example of proxy said. I used two proxies ip. I googled "free 
proxy 8080"

What is the expected output? What do you see instead?
To get url wanted. And if i have an exception, it would be nice to have a 
better explanation of what is the problem and how to fix it.

What version of the product are you using? On what operating system?
I'm using httplib 0.6.0 for python 2.5 on a MS Windows.

Please provide any additional information below.
I am developing an script that connect to government tax webservice, but my 
client has a proxy, so I must run it with a proxy configuration :)
I used 2 proxy free IP, and with one I get a good result (second test). -I 
can't use my clients proxy IP for another reason.-
So I tested it:
>>> import httplib2
>>> import socks
>>> httplib2.debuglevel = 4
>>> h = httplib2.Http(proxy_info = 
httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, '221.120.211.2', 8080))
>>> r, c = h.request("http://www.portablepython.com/")
connect: (www.portablepython.com, 80)
connect fail: ('www.portablepython.com', 80)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    r, c = h.request("http://www.portablepython.com/")
  File "build\bdist.win32\egg\httplib2\__init__.py", line 1129, in request
  File "build\bdist.win32\egg\httplib2\__init__.py", line 901, in _request
  File "build\bdist.win32\egg\httplib2\__init__.py", line 871, in 
_conn_request
  File "C:\Python25\lib\httplib.py", line 923, in getresponse
    method=self._method)
  File "C:\Python25\lib\httplib.py", line 324, in __init__
    self.fp = sock.makefile('rb', 0)
AttributeError: 'NoneType' object has no attribute 'makefile'

>>> h = httplib2.Http(proxy_info = 
httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, '66.76.90.0', 8080))
>>> r, c = h.request("http://www.portablepython.com/")
connect: (www.portablepython.com, 80)
send: 'GET / HTTP/1.1\r\nHost: www.portablepython.com\r\naccept-encoding: 
gzip, deflate\r\nuser-agent: Python-httplib2/$Rev$\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Thu, 03 Jun 2010 18:54:14 GMT
header: Server: Apache/2.2.3 (CentOS)
header: Cache-control: must-revalidate
header: Content-Length: 9681
header: Set-Cookie: trac_form_token=21dcd63575cc17c3f0f1c3bc; Path=/;
header: Set-Cookie: trac_session=60d4e55c83a02bc53c2f6f75; expires=Wed, 01-
Sep-2010 18:54:14 GMT; Path=/;
header: Connection: close
header: Content-Type: text/html;charset=utf-8
>>> 

Original issue reported on code.google.com by mati...@gmail.com on 3 Jun 2010 at 7:27

GoogleCodeExporter commented 8 years ago
I wrote httplib but I wanted to mean httplib2
Sorry

Original comment by mati...@gmail.com on 3 Jun 2010 at 7:28

GoogleCodeExporter commented 8 years ago
This should be fixed in trunk. Can you try and confirm that this doesn't repeat 
with 
code from trunk? Thanks!

Original comment by joe.gregorio@gmail.com on 3 Jun 2010 at 8:19

GoogleCodeExporter commented 8 years ago
I've downloaded trunk version.
Still got an exception with an explanation that is not self-explanatory (for me)

AttributeError: 'error' object has no attribute 'errno' (I'm pasting all 
traceback 
below)
I tried to debug it, with Eclipse, and the problem is here, in 
httplib2.__init__():
On this line: http://is.gd/cCDHl
            except socket.error, e:
                if e.errno == errno.ECONNREFUSED: # Connection refused
                    raise
Just because e has no attribute 'errno'.
So, I wrote a raise before if statement and this was the exception
e   error: (10060, 'Operation timed out')

I couldn't find where this socket exception is raised, sorry. But hope you 
have, with 
this, some extra data to use.

>>> h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 
'221.120.211.2', 8080))
>>> r, c = h.request("http://www.portablepython.com/")
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    r, c = h.request("http://www.portablepython.com/")
  File "C:\Python25\Lib\site-packages\httplib2\__init__.py", line 1135, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, 
body, headers, redirections, cachekey)
  File "C:\Python25\Lib\site-packages\httplib2\__init__.py", line 907, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, 
headers)
  File "C:\Python25\Lib\site-packages\httplib2\__init__.py", line 870, in 
_conn_request
    if e.errno == errno.ECONNREFUSED: # Connection refused
AttributeError: 'error' object has no attribute 'errno'

And this is with raise before if statement:
>>> r, c = h.request("http://www.portablepython.com/")
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    r, c = h.request("http://www.portablepython.com/")
  File "C:\Python25\lib\site-packages\httplib2\__init__.py", line 1136, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, 
body, headers, redirections, cachekey)
  File "C:\Python25\lib\site-packages\httplib2\__init__.py", line 908, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, 
headers)
  File "C:\Python25\lib\site-packages\httplib2\__init__.py", line 865, in 
_conn_request
    conn.request(method, request_uri, body, headers)
  File "C:\Python25\lib\httplib.py", line 866, in request
    self._send_request(method, url, body, headers)
  File "C:\Python25\lib\httplib.py", line 889, in _send_request
    self.endheaders()
  File "C:\Python25\lib\httplib.py", line 860, in endheaders
    self._send_output()
  File "C:\Python25\lib\httplib.py", line 732, in _send_output
    self.send(msg)
  File "C:\Python25\lib\httplib.py", line 699, in send
    self.connect()
  File "C:\Python25\lib\site-packages\httplib2\__init__.py", line 752, in connect
    raise socket.error, msg
error: (10060, 'Operation timed out')
>>> 

Still has problems, error: (10060, 'Operation timed out')

Original comment by mati...@gmail.com on 4 Jun 2010 at 6:19

GoogleCodeExporter commented 8 years ago
I'm having this problem too, even with the trunk version.

I do get a "connection refused" the first time trying to access an unresponsive 
server with the same Http object, but I get the AttributeError the second time:

First time:

  File "/Users/nicholas/LMI/xml_proxy/lib/python2.6/site-packages/httplib2/__init__.py", line 1132, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/Users/nicholas/LMI/xml_proxy/lib/python2.6/site-packages/httplib2/__init__.py", line 904, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/Users/nicholas/LMI/xml_proxy/lib/python2.6/site-packages/httplib2/__init__.py", line 862, in _conn_request
    conn.request(method, request_uri, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 874, in request
    self._send_request(method, url, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 911, in _send_request
    self.endheaders()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 868, in endheaders
    self._send_output()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 740, in _send_output
    self.send(msg)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 699, in send
    self.connect()
  File "/Users/nicholas/LMI/xml_proxy/lib/python2.6/site-packages/httplib2/__init__.py", line 749, in connect
    raise socket.error, msg
error: [Errno 61] Connection refused

Second time:

  File "/Users/nicholas/LMI/xml_proxy/lib/python2.6/site-packages/httplib2/__init__.py", line 1132, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/Users/nicholas/LMI/xml_proxy/lib/python2.6/site-packages/httplib2/__init__.py", line 904, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/Users/nicholas/LMI/xml_proxy/lib/python2.6/site-packages/httplib2/__init__.py", line 874, in _conn_request
    response = conn.getresponse()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 948, in getresponse
    method=self._method)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 329, in __init__
    self.fp = sock.makefile('rb', 0)
AttributeError: 'NoneType' object has no attribute 'makefile'

Original comment by nriley on 22 Jul 2010 at 4:47

GoogleCodeExporter commented 8 years ago
This is a bit drastic, but it works:

diff --git a/python2/httplib2/__init__.py b/python2/httplib2/__init__.py
--- a/python2/httplib2/__init__.py
+++ b/python2/httplib2/__init__.py
@@ -1154,6 +1154,8 @@
                             })
                     response.reason = "Bad Request"
             else:
+                if 'conn' in locals():
+                    del self.connections[conn_key]
                 raise

Original comment by nriley on 22 Jul 2010 at 5:11

GoogleCodeExporter commented 8 years ago

Original comment by joe.gregorio@gmail.com on 13 Feb 2011 at 3:47