Python中的摘要式身份验证?

bad*_*guy 4 python authentication ntlm digest

我正在尝试使用python从我的公司服务器访问页面。第一路径返回401:未授权(服务器确实需要域用户名/ pwd进行身份验证)。标头内容如下,它似乎支持3种身份验证协议,即Negotiate,NTLM和Digest,所以据我所知,我可以选择其中的一种,对吗?

Content-Type: text/html
Server: Microsoft-IIS/7.0
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
WWW-Authenticate: Digest qop="auth",algorithm=MD5-sess,nonce="+Upgraded+v184080dc2d18fe10d63520db505929b5b5b929ec98692ce010e80d6347b7a35d4027e59e277ac4fe1c257a95196071258a8e0797bf6129f76",charset=utf-8,realm="Digest"
X-Powered-By: ASP.NET
Date: Tue, 06 Aug 2013 09:24:44 GMT
Connection: close
Content-Length: 1293
Set-Cookie: LB-INFO=1065493258.20480.0000; path=/
Run Code Online (Sandbox Code Playgroud)

我正在使用以下python代码,但仍然出现401错误错误,有人可以告诉我如何实现吗?我应该使用NTLM吗?提前致谢!

p = urllib2.HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, self.url, username, password)
handler = urllib2.HTTPDigestAuthHandler(p)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)

f = opener.open(self.url)
Run Code Online (Sandbox Code Playgroud)

小智 15

另一种非常流行的 HTTP 身份验证形式是摘要式身份验证,Requests 也支持这种开箱即用的方式:

from requests.auth import HTTPDigestAuth
url = 'http://httpbin.org/digest-auth/auth/user/pass'
requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案 (2认同)

Cam*_*arr 5

urllib2 是python标准库,但不一定是HTTP请求的最佳工具。

我强烈建议您检出该requests程序包,并在此处找到身份验证教程:http : //docs.python-requests.org/en/latest/user/authentication/#digest-authentication

  • 谢谢,我用要求做到了!`requests.get(URL,auth = HttpNtlmAuth('domain \\ username',pwd))` (2认同)