發現的例子在HTTPProxyAuth使用這裏https://stackoverflow.com/a/8862633Python的請求庫結合HTTPProxyAuth與HTTPBasicAuth
但我希望有一個樣品上使用既HTTPProxyAuth和HTTPBasicAuth IE我需要通過一個服務器,用戶名和密碼代理和用戶名和密碼到網頁...
在此先感謝。
Richard
發現的例子在HTTPProxyAuth使用這裏https://stackoverflow.com/a/8862633Python的請求庫結合HTTPProxyAuth與HTTPBasicAuth
但我希望有一個樣品上使用既HTTPProxyAuth和HTTPBasicAuth IE我需要通過一個服務器,用戶名和密碼代理和用戶名和密碼到網頁...
在此先感謝。
Richard
不幸的是,HTTPProxyAuth
是HTTPBasicAuth
的孩子,並且覆蓋其行爲(請參閱requests/auth.py
)。
class HTTPBasicAndProxyAuth:
def __init__(self, basic_up, proxy_up):
# basic_up is a tuple with username, password
self.basic_auth = HTTPBasicAuth(*basic_up)
# proxy_up is a tuple with proxy username, password
self.proxy_auth = HTTPProxyAuth(*proxy_up)
def __call__(self, r):
# this emulates what basicauth and proxyauth do in their __call__()
# first add r.headers['Authorization']
r = self.basic_auth(r)
# then add r.headers['Proxy-Authorization']
r = self.proxy_auth(r)
# and return the request, as the auth object should do
return r
它不漂亮,但你可以在兩個代理和限制提供單獨的基本驗證憑據:
但是,你可以通過同時實現行爲的新類同時添加所需的頭部您的要求頁面網址。
例如:
proxies = {
"http": "http://myproxyusername:[email protected]:8080/",
"https": "http://myproxyusername:[email protected]:8080/",
}
r = requests.get("http://mysiteloginname:[email protected]", proxies=proxies)
什麼是你要求? –
我需要通過添加HTTPBasicAuth來構建鏈接中的示例,因爲我不僅需要通過帶有用戶名\密碼的代理來獲取代碼,還需要使用不同用戶名\密碼對目標網頁進行基本身份驗證。 – thylacine