开发者

Python urlopen windows authentication

I am not experienced with python and using the below code to open a url and read the response. I am getting an unauthorized error because the site uses Windows Authentication. Can someone provide a code sample on how to send in the user name and password?

response = ur开发者_如何学JAVAllib.request.urlopen(url, params.encode("ASCII"))
html = response.read()


Try using urllib2 and python-ntlm Some example code:

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())
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜