Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trap Authentication Errors in HTTP Request

Reply
Thread Tools

Trap Authentication Errors in HTTP Request

 
 
naugiedoggie
Guest
Posts: n/a
 
      09-10-2010
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,hea ders[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
 
Reply With Quote
 
 
 
 
naugiedoggie
Guest
Posts: n/a
 
      09-11-2010
On Sep 10, 12:09*pm, naugiedoggie <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Send Basic HTTP authentication credential in the first HTTP request Nacho Nachev ASP .Net Web Services 2 09-05-2012 08:49 PM
Trap run time errors in XSLT Rolf Kemper XML 0 10-20-2004 07:07 AM
Trap all errors Andrew Banks ASP .Net 3 04-23-2004 03:39 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM
Trap "connection pool" errors Sean Nolan ASP .Net 1 07-11-2003 02:59 PM



Advertisments