如何在 fiddler 中捕获 python https 流量?

rik*_*nin 5 python https fiddler python-requests

每当我尝试执行某些数据获取任务时,Python 都会出错。这只发生在我设置 fiddler 来解密 https 流量时。我已经尝试通过 127.0.0.1:8888 路由 python 流量,并且与 mozilla 相同,以便捕获其流量。我还安装了证书并通过 fiddler 信任它,我不确定我哪里出错了。

    raise SSLError(e, request=request)
    requests.exceptions.SSLError: HTTPSConnectionPool(host='google.com', port=443):
    Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFIC
    ATE_VERIFY_FAILED] certificate verify failed (_ssl.c:748)'),))
Run Code Online (Sandbox Code Playgroud)

以上是我尝试获取带有请求的页面时遇到的错误。

小智 6

TL;DR requests 库不使用 Windows 证书存储,它有自己的一个(根据https://bugs.python.org/issue28547)。这意味着默认情况下,您的提琴手 MITM 证书不可用于 python 请求。

您的选择是

  1. 禁用 SSL 验证(verify=False)
  2. 通过 REQUESTS_CA_BUNDLE 环境变量添加您的证书
  3. 明确添加您的提琴手证书(verify='\path\to\cert')

更多细节可以在http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification找到

另一方面,请求使用自己的证书包而不是平台提供的证书确实有点奇怪 - 特别是考虑到所有浏览器都乐于使用平台的。