如何使用 Python 请求执行 NTLM SSPI 身份验证? [英] How to use Python requests to perform NTLM SSPI authentication?
问题描述
我的目标是通过 NTLM 和 SSPI 对使用 Python 3.5.2 中的请求库 (2.11.1) 的客户端进行身份验证,这样用户就不必手动输入她的域凭据(用于登录到 PC).
My goal is to authenticate my client that uses the requests library (2.11.1) in Python 3.5.2 through NTLM with SSPI so that the user does not have to manually enter her domain credentials (used to login to the PC).
我发现了以下可能性,但都不适合我:
I have found the following possibilities, but none work for me:
HttpNtlmSspiAuth 在请求中引发异常:
导入请求从 requests_ntlm 导入 HttpNtlmAuth, HttpNtlmSspiAuth
import requests from requests_ntlm import HttpNtlmAuth, HttpNtlmSspiAuth
requests.get(site_url, auth=HttpNtlmSspiAuth())
requests.get(site_url, auth=HttpNtlmSspiAuth())
requests-sspi-ntlm 总是得到 401:
导入请求从 requests_sspi_ntlm 导入 HttpNtlmAuth
import requests from requests_sspi_ntlm import HttpNtlmAuth
session = requests.Session()session.auth = HttpNtlmAuth()session.get("http://ntlm_protected_site.com")
session = requests.Session() session.auth = HttpNtlmAuth() session.get("http://ntlm_protected_site.com")
而且 requests-negotiate-sspi 也会在请求中触发异常:
And requests-negotiate-sspi also triggers an exception in requests:
导入请求从 requests_negotiate_sspi 导入 HttpNegotiateAuth
import requests from requests_negotiate_sspi import HttpNegotiateAuth
r = requests.get('https://iis.contoso.com', auth=HttpNegotiateAuth())
r = requests.get('https://iis.contoso.com', auth=HttpNegotiateAuth())
我做错了吗?
推荐答案
requests-negotiate-sspi 对我有用.
我可能对 PO 有同样的问题,但我懒得尝试 PO 的解决方案并将 PO 的代码集成到我的中.谷歌帮助了我.如果有人遇到从 sspi.py ValueError: year 30828 is out of range
引发的相同异常,这是 requests-negotiate-sspi.请参阅此处:Github 问题
I probably had the same issue with PO, but I was too lazy to try PO's solution and integrate PO's code into mine. And Google helped me out. In case anyone encounters the same exception raised from sspi.py ValueError: year 30828 is out of range
, it's a known issue for python 3.6 of requests-negotiate-sspi. See here: Github-Issue
我通过使用 python 3.4 创建一个新的 conda 环境解决了这个问题.然后重新安装一些依赖以及requests-negotiate-sspi
,boom,一切正常.
I solved this by creating a new conda environment with python 3.4. Then reinstall some dependencies as well as requests-negotiate-sspi
, boom, all works.
这篇关于如何使用 Python 请求执行 NTLM SSPI 身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!