gpt4 book ai didi

python - 使用 python 和 python NTLM 浏览受 NTLM 保护的网站

转载 作者:太空宇宙 更新时间:2023-11-03 11:56:51 26 4
gpt4 key购买 nike

我的任务是创建一个脚本,该脚本登录到公司门户转到特定页面,下载该页面,将其与早期版本进行比较,然后根据所做的更改向特定人员发送电子邮件。最后一部分很简单,但给我带来最大麻烦的是第一步。

在使用 urllib2(我试图在 python 中执行此操作)连接失败后,经过大约 4 或 5 小时的谷歌搜索,我确定我无法连接的原因是网页上的 NTLM 身份验证。我已经尝试了一系列不同的连接过程,但都没有成功。基于NTLM example我做了:

import urllib2
from ntlm import HTTPNtlmAuthHandler

user = 'username'
password = "password"
url = "https://portal.whatever.com/"

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)

# create a header
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
header = { 'Connection' : 'Keep-alive', 'User-Agent' : user_agent}

response = urllib2.urlopen(urllib2.Request(url, None, header))

当我运行它(使用真实的用户名、密码和 url)时,我得到以下信息:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ntlm2.py", line 21, in <module>
response = urllib2.urlopen(urllib2.Request(url, None, header))
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 400, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 432, in error
result = self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 619, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:\Python27\lib\urllib2.py", line 400, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 432, in error
result = self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 619, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "C:\Python27\lib\urllib2.py", line 400, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 513, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 438, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 372, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 521, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized

对我而言,此跟踪最有趣的一点是最后一行显示返回了 401 错误。从我所拥有的read 401 错误是启动 NTLM 时发送回客户端的第一条消息。我的印象是 python-ntml 的目的是为我处理 NTLM 进程。那是错误的还是我只是错误地使用它?此外,我并不一定要为此使用 python,因此,如果有更简单的方法可以用另一种语言执行此操作,请告诉我(据我所见,谷歌搜索没有)。谢谢!

最佳答案

如果站点正在使用 NTLM 身份验证,则生成的 HTTPError 的 header 属性应该这样说:

>>> try:
... handle = urllib2.urlopen(req)
... except IOError, e:
... print e.headers
...
<other headers>
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM

关于python - 使用 python 和 python NTLM 浏览受 NTLM 保护的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6427039/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com