Urllib2 / add_password method

mrstephengross mrstephengross at hotmail.com
Fri Apr 28 13:20:01 EDT 2006


I'm working on learning how to use urllib2 to use a proxy server. I've
looked through the postings on this group, and it's been helpful. I
have not, however, found complete documentation on the add_password()
functions. Here's what I've got so far:

#========================================
import urllib2
url = 'http://some.server/form.php'

proxy_url = 'http://202.62.252.3:8080' # A publicly available anonymous
proxy server

proxy_handler = urllib2.ProxyHandler( {'http': proxy_url } )

proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username',
'password')

opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)

urllib2.install_opener(opener)

try:
  handler = urllib2.urlopen(url)
except urllib2.URLError:
  print "whoops!"
else:
  print handler.read()
#=======================================

It works, but I don't really know what I'm doing with the
proxy_auth_handler part. Specifying username and password make sense,
but I haven't found documentation on what 'realm' and 'host' are for.
Shouldn't username & password be sufficient?

Thanks,
--Steve (mrstephengross at hotmail.com)




More information about the Python-list mailing list