#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25

    urllib2 basic authentication


    I have been searching google for hours. I can't seem to get this to work right. I am trying to connect to a location that requires basic authentication to view the page. But I either get 401 errors or when i try to open the page and read it, it gives me nothing. I don't understand what i am doing wrong, here is my current code.

    Code:
    import urllib2, sys, re, base64
    from urlparse import urlparse
    theurl = 'http://example.com:81'
    username = 'test'
    password = 'XXXXXX'         # a great password
    
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()      # this creates a password manager
    passman.add_password(None, theurl, username, password)      # because we have put None at the start it will always use this username/password combination
    
    authhandler = urllib2.HTTPBasicAuthHandler(passman)                 # create the AuthHandler
    
    opener = urllib2.build_opener(authhandler)                                  # build an 'opener' using the handler we've created
    # you can use the opener directly to open URLs
    # *or* you can install it as the default opener so that all calls to urllib2.urlopen use this opener
    urllib2.install_opener(opener)
    
    for line in urllib2.urlopen("http://example.com:81"):
    	print line
    any ideas?
    Last edited by CyBerHigh; April 24th, 2005 at 11:19 PM.
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Did you try checking on ASPN?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    that is where i got that.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    For basic authentication the realm parameter should be set:
    Code:
    ...
    passman.add_password(my_realm, theurl, username, password)...
    If 'my_realm' is equal to 'AuthName' in .htaccess on the web server, it should work.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    i don't know the ralem of this, this program is to help me manage some thing through cpanel, but i want to automate a few things. I am not sure what my host has as the authname.

    and if i did, does that mean i don't create it with defualtRelm?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    On a normal web server with basic authentication it should work with default or named realms. Maybe there is more than basic authentication required.
    You could try authentication without authentication handler and opener as shown in the aspn example to get more information and control on what is sent and received during login.

IMN logo majestic logo threadwatch logo seochat tools logo