Eric
Eric

Reputation: 295

Python can't authenticate with ntlm

I am trying to parse a site on my intranet and when authenticating as below I get an error saying that authentication is required, which I have already done. Why am I still getting this 401 error?

Thanks in advance!

File "C:\Python27\lib\urllib2.py", line 531, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Authorization Required

import urllib2
from ntlm import HTTPNtlmAuthHandler

user = r'domain\myuser'
password = 'mypasswd'
url = 'http://myinternal.homepage'

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)

Upvotes: 2

Views: 2446

Answers (1)

MinimalMaximizer
MinimalMaximizer

Reputation: 392

Try not putthing the 'r' in front of 'domain\myuser'. I have used this without the 'r' and it works for me. One thing that helped me- ( i'm guessing you probably already did . . . just in case though) check the headers that the url returns to you. I did this with Mechanize http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat-sheet/ and based of the headers returned figured out I should be using NTLM auth ( as seen here). I also have a similar question How to 'convert' variable of type instance such that the variable can be used to authenticate when making system calls.

Upvotes: 1

Related Questions