Skip to content

Instantly share code, notes, and snippets.

@ianoxley
Created December 7, 2011 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianoxley/1443450 to your computer and use it in GitHub Desktop.
Save ianoxley/1443450 to your computer and use it in GitHub Desktop.
Access URL's from behind a proxy in Python
""" proxy.py
Wires up a proxy server so you can access URL's via Python.
Based on this answer from Stack Overflow: http://stackoverflow.com/a/35443
TODO:
1. use proper command line args via optparse / OptionParser
"""
import urllib2
def build_opener(proxy):
""" Returns an OpenerDirector wired up to use the proxy settings passed in the dictionary proxy
proxy should contain keys / values for:
- scheme - http
- url - the proxy server's URL
- username - the username for the proxy
- password - the password for the proxy
"""
passwd_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
passwd_mgr.add_password(None, proxy["url"], proxy["username"], proxy["password"])
proxy_handler = urllib2.ProxyHandler({proxy["scheme"]: proxy["url"]})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler(passwd_mgr)
return urllib2.build_opener(proxy_handler, proxy_auth_handler)
if __name__ == "__main__":
import sys
if len(sys.argv) > 4:
proxy = {}
proxy["scheme"], proxy["url"], proxy["username"], proxy["password"] = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]
print proxy
else:
print "Usage: ", sys.argv[0], "scheme proxy username password"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment