Proxy Authentication using urllib2

Andre Bocchini lists at
Sat Sep 20 01:48:57 CEST 2003

I'm having some trouble using proxy authentication.  I can't figure out 
how to authenticate with a Squid proxy.  I know for a fact the proxy is 
using Basic instead of Digest for the authentication.  I can 
authenticate just fine using Mozilla.  I've done some Google searches, 
but the closest piece of code I've is is for HTTPBasicAuthHandler:

# set up authentication info
authinfo = urllib2.HTTPBasicAuthHandler()
authinfo.add_password('realm', 'host', 'username', 'password')
proxy_support = urllib2.ProxyHandler({"http" : "http://ahad-haam:3128"})
# build a new opener that adds authentication and caching FTP handlers
opener = urllib2.build_opener(proxy_support, authinfo, 
# install it
f = urllib2.urlopen('')

Can anybody point me in the right direction to some more detailed 
documentation?  I haven't really been able to understand what I found 
in the Python Library Reference either.

I thought something like this might work, but is didn't:

proxy_handler = urllib2.ProxyHandler({"http" : "http://myproxy:3128"})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password(None, "myproxy", "myname", "mypass")
opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
f = urllib2.urlopen("")
data = f.readlines()

I always get a 407 error.

Well, thanks in advance.

-- Andre

More information about the Python-list mailing list