我正在尝试在我的 Intranet 上解析一个站点,并且在进行如下身份验证时,我收到一条错误消息,指出需要进行身份验证,我已经完成了。为什么我仍然收到此 401 错误?
提前致谢!
文件“C:\Python27\lib\urllib2.py”,第 531 行,在 http_error_default 中引发 HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Authorization Required
import urllib2
from ntlm import HTTPNtlmAuthHandler
user = r'domain\myuser'
password = 'mypasswd'
url = 'http://myinternal.homepage'
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
# create the NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
# create and install the opener
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)
# retrieve the result
response = urllib2.urlopen(url)
print(response)