[Tutor] Trapping HTTP Authentication Failure

Michael Powe michael at trollope.org
Sat Sep 11 13:39:24 CEST 2010


On Sat, Sep 11, 2010 at 01:09:31PM +0200, Evert Rol wrote:

> >>> My script to call a web service authenticates.  
> > 
> >> Sorry, but where is the (full) script? I missed an attachment or (preferably) a link.
> > 
> > Hello,
> > 
> > Sorry, the verb of the sentence is "authenticates," as in, "My script
> > ... authenticates."
> 
> Sorry, misread that.
> Although code does help :-). 

> I assume the rest of the code is just the loop around the items you
> want to fetch, calling getData each time with a new URL.

Yes, it's looping over a list.  Specifically, at startup the script
does a date detection and creates a list of identifiers for each week
of the year up to the point of invocation.  So, if it is week 36, I
get a list of 36 weeks.

Then, the data retrieval and processing takes place like this:

[processData(w) for w in weeks]

Where the processData function calls getData() and passes in the
current week to process.

[ code snipped ]
 
> > So, to restate the question, how can I trap an exception in the cases
> > in which authentication fails? 

[ 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.

> 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!  ;-)

Thanks.

mp

-- 
Michael Powe		michael at trollope.org		Naugatuck CT USA


Is it time for your medication or mine?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20100911/decb5454/attachment.pgp>


More information about the Tutor mailing list