Python requests.post 证书验证失败

标签 python ssl https python-requests

我有一个 python 应用程序,它通过 HTTPS 连接到网页 (wikis.xxx.yyy) 以从中检索和解析数据。我可以浏览网页,因为所需的证书链在浏览器中(我的系统使用 Ubuntu 14.04 LTS)。

$ python --version
Python 2.7.6

连接调用:

data = requests.post(URL, data=payload) 

因以下错误而失败:

requests.exceptions.SSLError: [Errno bad handshake] [('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')]

我不能使用 verify=False,因为如果我这样做,我会得到这个错误:

/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html InsecureRequestWarning)

我尝试将验证中的证书作为客户端证书 ( http://docs.python-requests.org/en/latest/user/advanced/ ) 传递:data = requests.post(radio_status_URL, data=payload, verify="/home/user/.cert/foo .pem") 但它没有连接,实际上也没有返回任何错误。

考虑到我需要将证书链安装到受信任的 Ubuntu CA 存储中,我按照以下说明进行操作:https://askubuntu.com/questions/73287/how-do-i-install-a-root-certificate 我添加的 foo.cert 证书格式如下:

-----BEGIN CERTIFICATE-----
<....>
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
<....>
-----END CERTIFICATE-----

使用 openssl 我可以看到证书链是正确的,我可以连接到 wikis.xxx.yyy :-)

$ openssl s_client -CApath ~/.cert/ -connect wikis.xxx.yyy:443
CONNECTED(00000003)
depth=2 DC = local, DC = windows, CN = RIM Root CA MCA01YKF
verify return:1
depth=1 DC = net, DC = rim, CN = RIM Subordinate CA MCA03YKF
verify return:1
depth=0 C = CA, ST = Ontario, L = Waterloo, O = BlackBerry, OU = BTS Operations, CN = wikis.xxx.yyy, emailAddress = no-reply@xxx.yyy

verify return:1
---
Certificate chain
 0 s:/C=CA/ST=Ontario/L=Waterloo/O=BlackBerry/OU=BTS Operations/CN=wikis.xxx.yyy/emailAddress=no-reply@xxx.yyy
   i:/DC=net/DC=rim/CN=RIM Subordinate CA MCA03YKF
---
Server certificate
-----BEGIN CERTIFICATE-----
<...>
-----END CERTIFICATE-----
subject=/C=CA/ST=Ontario/L=Waterloo/O=BlackBerry/OU=BTS Operations/CN=wikis.xxx.yyy/emailAddress=no-reply@xxx.yyy
issuer=/DC=net/DC=rim/CN=RIM Subordinate CA MCA03YKF
---
No client certificate CA names sent
---
SSL handshake has read 1810 bytes and written 609 bytes
---
New, TLSv1/SSLv3, Cipher is RC4-SHA
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : RC4-SHA
    Session-ID: FFD3D3EAC3807427D2D5E5BB2D70F064E2BB14218A368676A3225FBC5E5F1078
    Session-ID-ctx: 
    Master-Key: 930FBFB110BFC861E06C6A27DEBB15C2524A86F7EFC56693F4E52BA33F0AD236E5182169039909EC1BB3DE89C4C96B2B
    Key-Arg   : None
    PSK identity: None
    PSK identity hint: None
    SRP username: None
    Start Time: 1442413894
    Timeout   : 300 (sec)
    Verify return code: 0 (ok)

所以它可以工作并给出 0 的 ret 代码。不幸的是 python 不能做同样的事情,我不知道如何连接 :-(

我该如何解决这个问题?

最佳答案

从您上一个测试案例来看,wikis.xxx.yyy 使用 TLSv1.2,但是 Requests 模块需要稍作修改才能使用它。

请引用这篇文章: https://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/

有一个很好的插件可以做到这一点: https://toolbelt.readthedocs.org/en/latest/user.html#ssladapter

顺便说一句,如果您已经安装了 urllib3,请求将使用它,但是 urllib3 直到现在才支持 TLS v1.2。

关于Python requests.post 证书验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32611703/

相关文章:

python - 在 Python 中捕获 PyCharm 停止信号

python - 在 python 列表理解中添加额外的语句

python - 我应该使用 `==` 与 `(None, None)` 元组进行比较吗?

python - 使用 Python 字典替换/清理 Pandas DataFrame 中的数据

python - 如何使用 RequestBin 或类似工具检查 Python 请求中的 'verify' 设置

ssl - Kubernetes Ingress SSL 证书无效

azure - 基于 TLS/SSL 的 AMQP 协议(protocol)

java - 如何创建持久的 https 连接

android - 在 cordova/on android 应用程序中,使用 https 的请求失败但使用 http 的相同请求成功

java - 如何为没有域名的主机创建SSL证书?