This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: urllib2 silently returns None when auth_uri is mismatched
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: kbk Nosy List: jjlee, kbk, radeex
Priority: normal Keywords:

Created on 2003-10-09 12:11 by radeex, last changed 2022-04-10 16:11 by admin. This issue is now closed.

Messages (4)
msg18585 - (view) Author: Christopher Armstrong (radeex) Date: 2003-10-09 12:11
Code like the following, where the `uri' argument which
I pass to add_password is not actually the URI that the
web server gives me in its request for basic auth,
makes the return value of OpenDirector.open None. This
is either a code bug or a documentation bug. I expected
director.open to raise an error, but saying something
like "open will return None in these cases: ...." would
also be a valid solution, if one that's not as good,
IMO... the AuthHandler could raise an error once it
realizes that none of the URIs we have match, perhaps?

import urllib2

realm = "Linksys BEFW11S4 V4"

passman = urllib2.HTTPPasswordMgr()
passman.add_password(realm,
'NOT_WHAT_THE_SERVER_GIVES_ME', 'user', 'password')

auther = urllib2.HTTPBasicAuthHandler(passman)
getter = urllib2.HTTPHandler()

director = urllib2.OpenerDirector()

director.add_handler(auther)
director.add_handler(getter)


f = director.open('http://192.168.1.1/Status.htm')
print f, "is None"
msg18586 - (view) Author: John J Lee (jjlee) Date: 2003-10-29 23:56
Logged In: YES 
user_id=261020

It's a doc bug.  I'll add a doc patch later. 
 
Returning None from a handler means "I can't handle this, but 
another handler might".  So AuthHandler is correct to return 
None, and shouldn't raise an exception.  If you want to get a 
URLError in cases where all relevant handlers return None, 
you need to do: 
 
director.add_handler(UnknownHandler) 
 
(which build_opener will do for you, actually) 
msg18587 - (view) Author: John J Lee (jjlee) Date: 2003-11-30 22:18
Logged In: YES 
user_id=261020

Uploaded doc fix as patch 851752. 
msg18588 - (view) Author: Kurt B. Kaiser (kbk) * (Python committer) Date: 2004-07-11 02:16
Logged In: YES 
user_id=149084

patch 851752 applied

liburllib2.tex 1.20
History
Date User Action Args
2022-04-10 16:11:40adminsetgithub: 39388
2003-10-09 12:11:59radeexcreate