在此处找到有关 HTTPProxyAuth 使用的示例https://stackoverflow.com/a/8862633
但我希望有一个关于使用 HTTPProxyAuth 和 HTTPBasicAuth IE 的示例,我需要通过代理传递服务器、用户名和密码,并将用户名和密码传递给网页......
提前致谢。
理查德
在此处找到有关 HTTPProxyAuth 使用的示例https://stackoverflow.com/a/8862633
但我希望有一个关于使用 HTTPProxyAuth 和 HTTPBasicAuth IE 的示例,我需要通过代理传递服务器、用户名和密码,并将用户名和密码传递给网页......
提前致谢。
理查德
对于基本身份验证,您可以使用 python 的 Httplib2 模块。下面给出一个例子。有关更多详细信息,请查看此
>>>import httplib2
>>>h = httplib2.Http(".cache")
>>>h.add_credentials('name', 'password')
>>>resp, content = h.request("https://example.org/chap/2",
"PUT", body="This is text",
headers={'content-type':'text/plain'} )
我不认为 Httplib2 提供代理支持。检查链接-
它并不漂亮,但您可以在代理和受限页面 URL 中提供单独的 BasicAuth 凭据。
例如:
proxies = {
"http": "http://myproxyusername:mysecret@webproxy:8080/",
"https": "http://myproxyusername:mysecret@webproxy:8080/",
}
r = requests.get("http://mysiteloginname:myothersecret@mysite.com", proxies=proxies)
不幸的是,它是其行为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