[Tutor] Trapping HTTP Authentication Failure

Evert Rol evert.rol at gmail.com
Sat Sep 11 14:25:24 CEST 2010


<snip />

>> I'm not sure what you're exactly doing here, or what you're getting,
>> but I did get curious and dug around urllib2.py. Apparently, there is
>> a hardcoded 5 retries before the authentication really fails. So any
>> stack trace would be the normal stack trace times 5. Not the 30 you
>> mentioned, but annoying enough anyway (I don't see how it would fail
>> for every element in the loop though. Once it raises an exception,
>> the program basically ends).
> 
> It never throws an exception.  Or, if it does, something about the way
> I'm calling suppresses it.  IOW, I can put in a bogus credential and
> start the script and sit here for 5 minutes and see nothing.  Then ^C
> and I get a huge stacktrace that shows the repeated calls.  After the
> timeout on one element in the list, it goes to the next element, times
> out, goes to the next.

Ok, now I had to try and recreate something myself. So my processData is:

def processData(f):
   global overview_url
   overview_url = baseurl + f
   getData(authHeaders)

(f being a filename, out of a list of many). Other code same as yours.

It definitely throws a 401 exception after 5 retries. No time-outs, no long waits. In fact, a time-out would to me indicate another problem (it still should throw an exception, though). So, unless you're catching the exception in processData somehow, I don't see where things could go wrong. 
I assume you have no problem with correct credentials or simply using a webbrowser?



>> I don't know why it's hard-coded that way, and not just an option
>> with a default of 5, but that's currently how it is (maybe someone
>> else on this list knows?).
> 
> I don't know, but even if I could set it to 1, I'm not helped unless
> there's a way for me to make it throw an exception and exit the loop. 
> 
>> If that's what you're finding, perhaps the quickest way is to
>> subclass urllib2.HTTPBasicAuthHandler, and override the
>> http_error_auth_reqed method (essentially keeping it exactly the
>> same apart from the hard-coded 5).
> 
> Now there's a challenge!  ;-)

It'd be straightforward, but not solve your current problem, I'm afraid.



More information about the Tutor mailing list