Proxy Authentication using urllib2

Myles myles at geocities.com
Tue Sep 23 03:18:52 CEST 2003


Andre Bocchini <lists at andrebocchini.com> wrote in message news:<mailman.1064015428.25301.python-list at python.org>...
> 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 

A piece of code that works through a Squid proxy here - originally
clipped from comp.lang.python - possibly an eff-bot post if my dim
memory serves me correctly.

---- snip ----
import urllib2

proxy_info = {
    'user' : 'username',
    'pass' : 'password',
    'host' : "proxy.name.com",
    'port' : 80 # or 8080 or whatever
}
 
# build a new opener that uses a proxy requiring authorization
proxy_support = urllib2.ProxyHandler({"http" : \
   "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
 
# install it
urllib2.install_opener(opener)

# use it
f = urllib2.urlopen('http://www.python.org/')
print f.headers
print f.read()
---- snip ----

Regards, Myles.




More information about the Python-list mailing list