2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.