擅长:python、mysql、java
<p>不幸的是,<code>HTTPProxyAuth</code>是<code>HTTPBasicAuth</code>的子代,并重写其行为(请参见<code>requests/auth.py</code>)。</p>
<p>但是,您可以通过创建一个实现这两个行为的新类,将这两个必需的头添加到请求中:</p>
<pre><code>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
</code></pre>