当前位置: 首页 > 面试题库 >

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

宗穆冉
2023-03-14
问题内容

当我访问Internet时,我必须通过代理服务器。
代理服务器需要身份验证。我写了以下代码。

import requests
from requests.auth import HTTPProxyAuth

proxies = {"http":"192.168.20.130:8080"}
auth = HTTPProxyAuth("username", "password")

r = requests.get("http://www.google.co.jp/", proxies=proxies, auth=auth)

当代理服务器需要基本身份验证时,以上代码可以很好地工作。
现在,我想知道代理服务器需要摘要身份验证时该怎么做。
HTTPProxyAuth在摘要身份验证中似乎无效(r.status_code返回407)。


问题答案:

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

import requests
import requests.auth

class 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")

# HTTP
r = requests.get("http://www.google.co.jp/", proxies=proxies, auth=auth)
r.status_code # 200 OK

# HTTPS
r = requests.get("https://www.google.co.jp/", proxies=proxies, auth=auth)
r.status_code # 200 OK


 类似资料:
  • Dropwizard是否也支持摘要验证?我只找到了基本的身份验证和OAuth。这方面的示例代码会很好。 null

  • } 要获取令牌,我执行以下步骤: > 使用浏览器转到:http://localhost:9000/oauth/authorize?response_type=code&client_id=test&redirect_uri=http%3a%2f%2flocalhost%3a8080%2f&scope=write 首先,它将我重定向到一个登录表单,在那里我输入用户名和密码:admin abc 请帮我

  • 是否可以使用WLST脚本或通过其他自动化方式启用WebLogic摘要身份验证? 我正在使用正式的Oracle WebLogic docker映像,并在映像构建期间使用WLST脚本对其进行配置。但是,我没有找到使用WLST启用摘要密码的方法。此外,在管理控制台上手动启用它需要重新启动,这实际上会破坏容器,因为WebLogic进程是入口点。 如何在图像生成期间自动启用摘要?

  • 如何在邮递员中设置curl摘要身份验证? 例如 curl 命令: 我尝试在邮递员中使用摘要式身份验证以及基本身份验证。摘要身份验证返回 401,而基本身份验证返回 500。所以我想说基本身份验证不是正确的。 在digest auth中,我设置了以下选项。 其他一切都保持默认。我还选中了“将帮助程序数据保存到请求”复选框和“更新请求”按钮。 仍然得到401。 根据响应,我得到了一个、和一个。我以前没

  • 问题内容: 我收到错误 NOAUTH必需的身份验证 。我的laravel版本是5.3,我正在使用predis 1.1.1连接redis。 在etc / redis / redis.conf中,我有: 在.env文件中 在config / database.php中,我有: 我通过以下方式连接redis: 并像这样使用它: 因此,当我注释掉并将密码发送为null时,它可以工作,但在密码到位后却无法工

  • 我们正在开发一个iOS应用程序,它使用谷歌来验证firebase。根据https://www . Firebase . com/docs/IOs/guide/user-auth . html # section-log in Firebase说,auth令牌每24小时过期一次。我们想知道以下场景是否是我们需要考虑的: 用户通过Google和Firebase进行身份验证 我们的应用程序将获得一个24

  • 问题内容: System.setProperty(“http.proxySet”, “true”); System.setProperty("java.net.useSystemProxies”, “true”); System.setProperty(“http.proxyHost”, “192.168.1.103”); System.setProperty(“http.proxyPort”,

  • 问题内容: 我正在尝试使用urllib.request.urlopen()打开一个网站(我在公司代理后面),但出现错误: 我可以在urllib.request.getproxies()中找到该代理,但是如何指定用于该代理的用户名和密码?我在官方文档中找不到解决方案。 问题答案: