使用 HTTPS 的 charles 代理检查 Python 个请求

check Python requests with charles proxy for HTTPS

我想使用 charles 代理调试一些 python 请求。

我需要在通话中包含 charles 的证书,但没有用

import requests
endpoint_url = 'https://www.httpsnow.org/'
r = requests.get(endpoint_url, verify=True, cert='/Users/iosdev/DopPy/charles.crt')

print "empexo"
print r

我在Charles上添加了https地址,

我得到查尔斯:

SSLHandshake: Remote host closed connection during handshake

在 python 上有错误的日志

empexo
Traceback (most recent call last):
  File "/Users/iosdev/DopPy/GetCelebs.py", line 15, in <module>
    r = requests.get(endpoint_url, verify=True, cert='/Users/iosdev/DopPy/charles.crt')
  File "/Users/iosdev/VenvPY26/lib/python2.6/site-packages/requests/api.py", line 65, in get
    return request('get', url, **kwargs)
  File "/Users/iosdev/VenvPY26/lib/python2.6/site-packages/requests/api.py", line 49, in request
    response = session.request(method=method, url=url, **kwargs)
  File "/Users/iosdev/VenvPY26/lib/python2.6/site-packages/requests/sessions.py", line 461, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/iosdev/VenvPY26/lib/python2.6/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/Users/iosdev/VenvPY26/lib/python2.6/site-packages/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [Errno 336265225] _ssl.c:341: error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib

Process finished with exit code 1

我在对类似问题进行故障排除时发现了此线程。在我 运行 到 cert 参数的场景中,当应该使用 verify 参数时,它被用来定义“.crt”文件的路径。

正确的用法最终看起来像:

requests.get(endpoint_url, verify='/path/to/file.crt')

有关更多详细信息,请参阅请求的文档:https://2.python-requests.org/en/v1.1.0/user/advanced/#ssl-cert-verification


顺便说一句,我发现使用 Request 的能力通过 REQUESTS_CA_BUNDLE 环境变量指定“.crt”的路径在使用 Charles Proxy 进行本地调试时更有效。

运行 类似于 shell 中的以下内容省去了为每个请求调用指定 Charles 的“.crt”的路径:

REQUESTS_CA_BUNDLE=/path/to/file.crt
export REQUESTS_CA_BUNDLE