[Tutor] Trapping HTTP Authentication Failure

Michael Powe michael at trollope.org
Sat Sep 11 11:58:21 CEST 2010


On Sat, Sep 11, 2010 at 01:28:26AM +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."

But I can show the authentication portion.

8<-------------- start -------------------->8

# 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)
    

# Retrieves the data    
def getData(authHeaders) :
    opener = createOpener(createAuthenticationHandler(createPasswordManager(authHeaders)))
    data = opener.open(overview_url)
    return data
8<--------------- end ------------------------>8

So, to restate the question, how can I trap an exception in the cases
in which authentication fails? 

Right now, the whole script is complete and working (thanks for your
help with my other exception-handling question).  Except for the case
of bad credentials.  The use case is that the user misspells a
username or password or puts in a wrong account information.  Then, I
don't want them to sit for 10 minutes while the script makes 30 data
connections, retries and fails each time.

Thanks.

mp    

-- 
Michael Powe		michael at trollope.org		Naugatuck CT USA
I hate a fellow whom pride, or cowardice, or laziness drives into a
corner, and who does nothing when he is there but sit and <growl>; let
him come out as I do, and <bark>. -- Samuel Johnson
-------------- 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/a0a7adbf/attachment.pgp>


More information about the Tutor mailing list