urllib2 http authorization question
Troels Therkelsen
t_therkelsen at hotmail.com
Sun Dec 14 13:06:45 EST 2003
In article <slrnbtp86j.l9u.matt at overlook.homelinux.net>, Matthew Wilson wrote:
[snip]
>>>> lookup.mr814('username', 'password')
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "lookup.py", line 13, in mr814
> for line in urllib2.urlopen(req):
> File "/usr/lib/python2.3/urllib2.py", line 136, in urlopen
> return _opener.open(url, data)
> File "/usr/lib/python2.3/urllib2.py", line 333, in open
> '_open', req)
> File "/usr/lib/python2.3/urllib2.py", line 313, in _call_chain
> result = func(*args)
> File "/usr/lib/python2.3/urllib2.py", line 849, in http_open
> return self.do_open(httplib.HTTP, req)
> File "/usr/lib/python2.3/urllib2.py", line 843, in do_open
> return self.parent.error('http', req, fp, code, msg, hdrs)
> File "/usr/lib/python2.3/urllib2.py", line 359, in error
> return self._call_chain(*args)
> File "/usr/lib/python2.3/urllib2.py", line 313, in _call_chain
> result = func(*args)
> File "/usr/lib/python2.3/urllib2.py", line 419, in http_error_default
> raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
> urllib2.HTTPError: HTTP Error 401: Unauthorized
>>>> lookup.mr814('admin', '54wins')
> '66.72.200.255'
To me, but maybe this is a copy-n-paste error, it looks like you aren't calling
lookup.mr814() with exactly the same args:
>>> lookup.mr814('username', 'password')
versus
>>> lookup.mr814('admin', '54wins')
?
>
> This is baffling me. Does the webserver ignore the AUTHORIZATION header
> on the first request? Should I do something like this:
>
>>>> try:
> ... lookup.mr814('admin', '54wins')
> ... except urllib2.HTTPError: #I only want to catch 401 here, but how?
> ... print "I got back the 401 error. Trying again..."
> ... lookup.mr814('admin', '54wins')
> ... except:
> ... print "something else went wrong."
>
> If that is recommended, how do I distinguish between 401 errors, vs
> other errors?
I don't know if it's recommended, but take a look at the source for the
urllib2.HTTPError class, at the definition of __str__ (which is used by the
traceback system):
def __str__(self):
return 'HTTP Error %s: %s' % (self.code, self.msg)
In other words, the actual HTTP error code is in self.code... you could check
for that something like this:
try:
lookup.mr814('admin', '54wins')
except urllib2.HTTPError, err:
if err.code != 401:
raise
>
> Thanks for the help.
I hope the above helps,
/Troels Therkelsen
More information about the Python-list
mailing list