Created
March 21, 2012 17:03
-
-
Save wynemo/2149567 to your computer and use it in GitHub Desktop.
urllib2 basic auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# quoted from http://www.wkoorts.com/wkblog/2008/10/27/python-proxy-client-connections-requiring-authentication-using-urllib2-proxyhandler/ | |
# urllib2_proxy_handler.py | |
# | |
# Author: Wayne Koorts | |
# Date: 27/10/2008 | |
# | |
# Example for using urllib2.urlopen() with a proxy server requiring authentication | |
import urllib2 | |
uri = "http://www.python.org" | |
http_proxy_server = "someproxyserver.com" | |
http_proxy_port = "3128" | |
http_proxy_realm = http_proxy_server # Worked in my (limited) testing environment. | |
http_proxy_user = "username" | |
http_proxy_passwd = "password" | |
# Next line = "http://username:[email protected]:3128" | |
http_proxy_full_auth_string = "http://%s:%s@%s:%s" % (http_proxy_user, | |
http_proxy_passwd, | |
http_proxy_server, | |
http_proxy_port) | |
def open_url_no_proxy(): | |
urllib2.urlopen(uri) | |
print "Apparent success without proxy server!" | |
def open_url_installed_opener(): | |
proxy_handler = urllib2.ProxyHandler({"http": http_proxy_full_auth_string}) | |
opener = urllib2.build_opener(proxy_handler) | |
urllib2.install_opener(opener) | |
urllib2.urlopen(uri) | |
print "Apparent success through proxy server!" | |
if __name__ == "__main__": | |
open_url_no_proxy() | |
open_url_installed_opener() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment