2011-08-31 159 views
1

我沒有經驗與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://stackoverflow.com/questions/909658/windows-authentication-with-python-和的urllib2 –

回答

2

嘗試使用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())