Search code examples
pythonurllib2httplib

urllib2.proxyhandler in python 2.5


In windows XP, python 2.5 and 2.6 I tested the following code:

import urllib2
proxy= urllib2.ProxyHandler({'http': '127.0.0.1:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
urllib2.urlopen('http://www.google.com/')

In the above code I get a BadStatusLine exception from line 349 of httplib.py.

I have a proxy running at 127.0.0.1:8080 which works (I can set a browser to use it with proxyswitchy, and when it's on I can get to sites which are blocked when it's off [in China]).

if I change it to a socks proxy,

proxy= urllib2.ProxyHandler({'socks': '127.0.0.1:8080'})

Then the proxy is not used at all.

I got the code from the question at Proxy with urllib2 and it's almost exactly the same - what could be going wrong?

Update: urllib2 doesn't support socks proxies.

Eventually got it working with curl:

c = pycurl.Curl()

#stupid GFW
if settings.CHINA:
    c.setopt(pycurl.PROXY, '127.0.0.1')
    c.setopt(pycurl.PROXYPORT, 8087)
    c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5)

Solution

  • The urllib2 ProxyHandler is not designed to support the SOCKS protocol. Perhaps this answer would help.