Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Authentication with urllib2

Tags:

python

So I'm trying to download a file from a site called vsearch.cisco.com with python

[python]

#Connects to the Cisco Server and Downloads files at the URL specified

import urllib2

#Define Useful Variables

url = 'http://vsearch.cisco.com'
username = 'xxxxxxxx'
password = 'xxxxxxxx'
realm = 'CEC'

# Begin Making connection

# Create a Handler -- Also could be where the error lies

handler = urllib2.HTTPDigestAuthHandler()
handler.add_password(realm,url,username,password)

# Create an Opener

opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)

try:
    urllib2.urlopen(url)
    print f.read()

except urllib2.HTTPError, e:
    print e.code
    print e.header

[/python]

My error is ValueError: AbstractDigestAuthHandler doesn't know about basic

I've tried using Basic HTML Authorization handlers and even HTTPS handlers. Nothing gives me access. This error is different from all the other errors however. The other errors are simply 401 HTML errors

Any suggestions on how to do this?

like image 545
webgoudarzi Avatar asked Feb 28 '23 14:02

webgoudarzi


1 Answers

A "password manager" might help:

    mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    mgr.add_password(None, url, user, password)        
    urllib2.build_opener(urllib2.HTTPBasicAuthHandler(mgr),
                         urllib2.HTTPDigestAuthHandler(mgr))
like image 180
Alex Martelli Avatar answered Mar 09 '23 00:03

Alex Martelli