如何使用python请求模块传递代理身份验证(需要摘要身份验证)

如何使用python请求模块传递代理身份验证(需要摘要身份验证),第1张

如何使用python请求模块传递代理身份验证(需要摘要身份验证)

我编写了可用于代理身份验证的类(基于摘要身份验证)。
我从request.auth.HTTPDigestAuth借用了几乎所有代码。

import requestsimport requests.authclass HTTPProxyDigestAuth(requests.auth.HTTPDigestAuth):    def handle_407(self, r):        """Takes the given response and tries digest-auth, if needed."""        num_407_calls = r.request.hooks['response'].count(self.handle_407)        s_auth = r.headers.get('Proxy-authenticate', '')        if 'digest' in s_auth.lower() and num_407_calls < 2: self.chal = requests.auth.parse_dict_header(s_auth.replace('Digest ', '')) # Consume content and release the original connection # to allow our new request to reuse the same one. r.content r.raw.release_conn() r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url) r.request.send(anyway=True) _r = r.request.response _r.history.append(r) return _r        return r    def __call__(self, r):        if self.last_nonce: r.headers['Proxy-Authorization'] = self.build_digest_header(r.method, r.url)        r.register_hook('response', self.handle_407)        return r

用法:

proxies = {    "http" :"192.168.20.130:8080",    "https":"192.168.20.130:8080",}auth = HTTPProxyDigestAuth("username", "password")# HTTPr = requests.get("http://www.google.co.jp/", proxies=proxies, auth=auth)r.status_pre # 200 OK# HTTPSr = requests.get("https://www.google.co.jp/", proxies=proxies, auth=auth)r.status_pre # 200 OK


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5647710.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存