Drew
Drew

Reputation: 421

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 = urllib.request.urlopen(url, params.encode("ASCII"))
html = response.read()

Upvotes: 1

Views: 7427

Answers (1)

bpgergo
bpgergo

Reputation: 16037

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

Upvotes: 2

Related Questions