如何在Python的请求模块中指定多个auth?

时间:2016-08-02 04:29:41

标签: python authentication python-requests

我想通过需要NTLM身份验证的代理访问BASIC身份验证受保护的网站。我正在使用Python的请求模块来访问该网站。如何为请求模块中的请求指定多个身份验证?即我需要为原始网站提供代理身份验证和BASIC凭据的NTLM凭据。我使用以下代码:

import requests
from requests_ntlm import HttpNtlmAuth
proxies = {'https': 'https://myproxy.com:8080', 'http': 'http://myproxy.com:8080'}
ntlm_auth = HttpNtlmAuth('ntlm_username','ntlm_secret')
# how to provide the credentials (BASIC auth) required by the actual website?
r = requests.get("https://myprotectedresource.com",auth=ntlm_auth, proxies=proxies) 

2 个答案:

答案 0 :(得分:0)

要在代理中使用HTTP Basic Auth,请使用AccessToken.findForRequest = function(req, options, cb) { } 语法:

http://user:password@host:port/

答案 1 :(得分:0)

尝试添加HTTPBasicAuth,如下所示:

import requests
from requests_ntlm import HttpNtlmAuth
from requests.auth import HTTPBasicAuth

proxies = {'https': 'https://myproxy.com:8080', 'http': 'http://myproxy.com:8080'}
auth = (HttpNtlmAuth('ntlm_username','ntlm_secret'), HTTPBasicAuth('user_name', 'user_password'))
r = requests.get("https://myprotectedresource.com",auth=auth, proxies=proxies)