1

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()
Drew
  • 421
  • 8
  • 16

1 Answers1

2

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())
bpgergo
  • 15,669
  • 5
  • 44
  • 68