Trap Authentication Errors in HTTP Request

N

naugiedoggie

Hello,

I have a script that authenticates to a web service provider to
retrieve data. This script provides an authentication header built in
a very basic way like this:

<code>
# Creates an authentication object with the credentials for a given
URL
def createPasswordManager(headers) :
passwordManager = urllib2.HTTPPasswordMgrWithDefaultRealm()

passwordManager.add_password(None,overview_url,headers[0],headers[1])
return passwordManager

# Creates an authentication handler for the authentication object
created above
def createAuthenticationHandler(passwordManager) :
authenticationHandler =
urllib2.HTTPBasicAuthHandler(passwordManager)
return authenticationHandler

# Creates an opener that sets the credentials in the Request
def createOpener(authHandler) :
return urllib2.build_opener(authHandler)
</code>

This script makes multiple calls for data. I would like to trap an
exception for authentication failure so that it doesn't go through its
entire list of calls when there's a problem with the login. The
assumption is that if there is a login failure, the script is using
incorrect authentication information.

I have the call for data retrieval wrapped in try/except, to catch
HTTPError, but apparently no '401' is explicitly thrown when
authentication fails. And I don't see an explicit Exception that is
thrown in urllib2 for handling these failures.

How can I achieve my goal of trapping these exceptions and exiting
cleanly?

Thanks.

mp
 
N

naugiedoggie

Hello,

I have a script that authenticates to a web service provider to
retrieve data.  This script provides an authentication header built in
a very basic way like this:

The answer is that there is something whacked in the Windoze
implementation for urllib2.

It turns out that the script works fine when run in a linux console.
'401' error is trapped as expected by an exception handler. In
Winblows, the builtin handler for authentication is supposed to take a
dump after 5 retries, but this seems to not happen. The retries
continue until a recursion exception is fired. At this point the
script dumps back to the console. An exception handler for Exception
will catch this.

Thanks.

mp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top