urllib2, proxy, basic authentication

Albert Chin-A-Young china at the.web.com
Mon Dec 4 21:35:00 EST 2000


I'm trying to use urlopen in urllib2.py with an HTTP proxy to connect
to a site requiring BASIC authentication. My code follows:

  import os, sys, urllib2
  from urllib2 import urlopen

  os.environ['http_proxy'] = 'http://[PROXY_HOST]:5865'

  authinfo = urllib2.HTTPBasicAuthHandler ()
  authinfo.add_password ('[REALM]', 'http://[URL]/', '[LOGIN]',
  '[PASSWORD]')

  opener = urllib2.build_opener (authinfo)
  urllib2.install_opener (opener)

  url = urlopen ('http://[URL]/')
  print url.info ()
  url.close ()

This is not working. Does urllib2 support this configuration?

    File "/tmp/a.py", line 15, in ?
      url = urlopen ('http://updates.thewrittenword.com/')
    File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
  line 137, in urlopen
      return _opener.open(url, data)
    File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
  line 325, in open
      '_open', req)
    File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
  line 304, in _call_chain
      result = func(*args)
    File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
  line 764, in http_open
      return self.parent.error('http', req, fp, code, msg, hdrs)
    File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
  line 351, in error
      return self._call_chain(*args)
    File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
  line 304, in _call_chain
      result = func(*args)
    File "/opt/TWWfsw/pkgutils12/lib/python20/lib/python2.0/urllib2.py",
  line 430, in http_error_default
      raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
  urllib2.HTTPError: HTTP Error 401: Authorization Required

-- 
albert chin (china @at@ thewrittenword .dot. com)



More information about the Python-list mailing list