How to specify an authenticated proxy for a python http connection?

Rehan Khwaja picture Rehan Khwaja · Aug 29, 2008 · Viewed 83.7k times · Source

What's the best way to specify a proxy with username and password for an http connection in python?

Answer

bernhardrusch picture bernhardrusch · Aug 29, 2008

This works for me:

import urllib2

proxy = urllib2.ProxyHandler({'http': 'http://
username:password@proxyurl:proxyport'})
auth = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
urllib2.install_opener(opener)

conn = urllib2.urlopen('http://python.org')
return_str = conn.read()