简体   繁体   English

Python无法通过ntlm进行身份验证

[英]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. 我正在尝试解析我的Intranet上的站点,并在按如下所示进行身份验证时收到一条错误消息,要求进行身份验证,这已经完成。 Why am I still getting this 401 error? 为什么我仍然收到此401错误?

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 http_error_default中的文件“ C:\\ Python27 \\ lib \\ urllib2.py”,第531行,引发HTTPError(req.get_full_url(),代码,msg,hdrs,fp)urllib2.HTTPError:HTTP错误401:需要授权

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)

Try not putthing the 'r' in front of 'domain\\myuser'. 尽量不要将“ r”放在“ domain \\ myuser”前面。 I have used this without the 'r' and it works for me. 我使用了没有'r'的东西,它对我有用。 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. 帮助我的一件事-(我想您可能已经做过……只是为了以防万一)检查url返回给您的标头。 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). 我使用Mechanize http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat-sheet/进行了此操作,根据返回的标头确定我应该使用NTLM auth(如此处所示)。 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 . 我也有一个类似的问题, 如何“转换”类型实例的变量,以便在进行系统调用时可以使用该变量进行身份验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM