using httplib, urllib with proxy authentication

Paul Moore paul.moore at atosorigin.com
Tue Apr 10 08:24:45 EDT 2001


Jeremy Lamplough <jeremy.lamplough at motorola.com> writes:
> Is it possible to use these libraries with proxies that require password
> authentication?  (I'm currently using python 1.5.2).  Do I need to upgrade?

You need to use urllib2, and Python 2.1 (earlier versions have bugs -
the 2.1 version as of today (beta 2) still has a bug, but I have
reported it, along with a fix, so I hope it will be fixed in the
release).

Sample code:

----
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)
 
f = urllib2.urlopen('http://www.python.org/')
print f.headers
print f.read()
----

Paul.




More information about the Python-list mailing list