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

时间:2015-03-06 00:27:32

标签: python request charles-proxy

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

我需要在电话中包含查尔斯证书,但不能正常工作

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地址,

我上了Charles:

  

SSLHandshake:握手期间远程主机关闭连接

并在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

1 个答案:

答案 0 :(得分:4)

我在解决类似问题时找到了这个帖子。在我遇到cert参数时遇到的verify参数被用来定义{。{1}}参数的路径时使用requests.get(endpoint_url, verify='/path/to/file.crt') 参数。

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

REQUESTS_CA_BUNDLE

有关详细信息,请参阅请求文档:http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification

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

在shell中运行类似下面的内容,可以为每个请求调用指定查询“.crt”的路径:

{{1}}