Trap Authentication Errors in HTTP Request

Discussion in 'Python' started by naugiedoggie, Sep 10, 2010.

  1. naugiedoggie

    naugiedoggie Guest

    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
     
    naugiedoggie, Sep 10, 2010
    #1
    1. Advertising

  2. naugiedoggie

    naugiedoggie Guest

    SOLVED: Re: Trap Authentication Errors in HTTP Request

    On Sep 10, 12:09 pm, naugiedoggie <> 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
     
    naugiedoggie, Sep 11, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Nolan

    Trap "connection pool" errors

    Sean Nolan, Jul 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    408
    David Browne
    Jul 11, 2003
  2. Andrew Banks

    Trap all errors

    Andrew Banks, Apr 22, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    438
    Rick Spiewak
    Apr 23, 2004
  3. Rolf Kemper

    Trap run time errors in XSLT

    Rolf Kemper, Oct 20, 2004, in forum: XML
    Replies:
    0
    Views:
    512
    Rolf Kemper
    Oct 20, 2004
  4. SalP
    Replies:
    3
    Views:
    430
    bruce barker \(sqlwork.com\)
    May 17, 2006
  5. Nacho Nachev
    Replies:
    2
    Views:
    954
Loading...

Share This Page