urllib2 http authentication/redirection/cookie issue

Neuberger, Sheldon N. sneuberger at mitre.org
Mon Jul 14 17:15:42 EDT 2008


I have a tricky situation here with auth/redirection/cookies and I
think I am messing something up with the handler.

I am trying to open a site http://foo.example.com which redirects
(status 302) to https://bar.example.com/------------ where the dashes
represent lots of subdirectories and parameters.  This second site,
bar.example.com, is the site which requires authorization (status 401).
Upon authorization, the second site will give a cookie or two then
redirect (status 302) to the original site which will then load because
of the cookie. 

The problem is that when I run my code, I still get an HTTP 401
exception. Any help would be much appreciated.

Code and traceback follows.

Regards,
Sheldon Neuberger


----------------------
cj = cookielib.LWPCookieJar()
cookie_handler = urllib2.HTTPCookieProcessor(cj)

passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
passmgr.add_password(None, 'https://bar.example.com',
                           'username', 'password')
auth_handler = urllib2.HTTPBasicAuthHandler(passmgr)

opener = urllib2.build_opener(cookie_handler, auth_handler)
urllib2.install_opener(opener)

html = urllib2.urlopen('http://foo.example.com/qux/')

---------------------

Traceback (most recent call last):
  File "dts.py", line 21, in <module>
    html = urllib2.urlopen('http://foo.example.com/qux/')
  File "C:\Python25\lib\urllib2.py", line 124, in urlopen
    return _opener.open(url, data)
  File "C:\Python25\lib\urllib2.py", line 387, in open
    response = meth(req, response)
  File "C:\Python25\lib\urllib2.py", line 498, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python25\lib\urllib2.py", line 419, in error
    result = self._call_chain(*args)
  File "C:\Python25\lib\urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "C:\Python25\lib\urllib2.py", line 582, in http_error_302
    return self.parent.open(new)
  File "C:\Python25\lib\urllib2.py", line 387, in open
    response = meth(req, response)
  File "C:\Python25\lib\urllib2.py", line 498, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python25\lib\urllib2.py", line 425, in error
    return self._call_chain(*args)
  File "C:\Python25\lib\urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "C:\Python25\lib\urllib2.py", line 506, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080714/bcca4300/attachment.html>


More information about the Python-list mailing list