简体   繁体   English

使用urllib2进行Python身份验证

[英]Python Authentication with urllib2

So I'm trying to download a file from a site called vsearch.cisco.com with python 所以我正在尝试使用python从名为vsearch.cisco.com的站点下载文件

[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 我的错误是ValueError:AbstractDigestAuthHandler不知道基本的

I've tried using Basic HTML Authorization handlers and even HTTPS handlers. 我尝试过使用Basic HTML Authorization处理程序甚至HTTPS处理程序。 Nothing gives me access. 什么都没有让我访问。 This error is different from all the other errors however. 但是,此错误与所有其他错误不同。 The other errors are simply 401 HTML errors 其他错误只是401 HTML错误

Any suggestions on how to do this? 有关如何做到这一点的任何建议?

A "password manager" might help: “密码管理器”可能有所帮助:

    mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    mgr.add_password(None, url, user, password)        
    urllib2.build_opener(urllib2.HTTPBasicAuthHandler(mgr),
                         urllib2.HTTPDigestAuthHandler(mgr))

至于我在测试中尝试的内容( http://devel.almad.net/trac/django-http-digest/browser/djangohttpdigest/tests/test_simple_digest.py ),您的网址中出现错误 - 要使其正常工作,我已经包含了http:// part,而不仅仅是主机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM