Python urlopen windows身份验证

问题描述:

我没有经验与python和使用下面的代码来打开一个url并阅读响应。我收到未经授权的错误,因为该网站使用Windows身份验证。有人可以提供关于如何发送用户名和密码的代码示例吗?Python urlopen windows身份验证

response = urllib.request.urlopen(url, params.encode("ASCII")) 
html = response.read() 
+0

什么是确切的错误消息? – bpgergo

+0

看这里:Windows的认证用的Python和-的urllib2] [1] [1]:http://*.com/questions/909658/windows-authentication-with-python-和的urllib2 –

尝试使用urllib2python-ntlm一些示例代码:

import urllib2 
from ntlm import HTTPNtlmAuthHandler 

user = 'DOMAIN\User' 
password = "Password" 
url = "http://ntlmprotectedserver/securedfile.html" 

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.read())