The best way for a proxy server to require authentication is to use urllib2 to create a custom url opener, and then use this to create all the requests you want to go through the proxy. Please note in particular that you probably do not want to embed a proxy password in the python url or source code (unless it's just a quick hack).
import urllib2 def get_proxy_opener(proxyurl, proxyuser, proxypass, proxyscheme="http"): password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, proxyurl, proxyuser, proxypass) proxy_handler = urllib2.ProxyHandler({proxyscheme: proxyurl}) proxy_auth_handler = urllib2.ProxyBasicAuthHandler(password_mgr) return urllib2.build_opener(proxy_handler, proxy_auth_handler) if __name__ == "__main__": import sys if len(sys.argv) > 4: url_opener = get_proxy_opener(*sys.argv[1:4]) for url in sys.argv[4:]: print url_opener.open(url).headers else: print "Usage:", sys.argv[0], "proxy user pass fetchurls..."
In a more complex program, you can separate these components as needed (for example, using only one password manager for the life of the application). The python documentation has more examples of how to do complex things with urllib2 , which can also be useful.
gz. Aug 29 '08 at 22:52 2008-08-29 22:52
source share