开发者

Python urlopen windows authentication

开发者 https://www.devze.com 2023-04-01 10:42 出处:网络
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 cod

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

精彩评论

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