tornado下https配置

问题背景

越来越多的网站已经支持https,相比于http更安全。尤其有的开发网站只支持https,例如微信公众平台。
这里暂时不提tornado如何搭建https服务,回头有时间再记一下。

SSLError

可以用AsyncHTTPClient发送一个简单的https请求

https_url = "https://path"      
https_client = AsyncHTTPClient()
response = yield YieldTask(token_client.fetch, access_token_url)

结果出现了如下问题

ssl.SSLError: [Errno 1] _ssl.c:510: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

参考tornado rejects valid SSL certificates

这个原因是因为证书设置不正确,那么我们可以通过下面的操作给AsyncHTTPClient设置证书。

 import certifi
 AsyncHTTPClient.configure(None, defaults=dict(ca_certs=certifi.where()))

但是这个设置以后,会发现虽然不报错了,但是请求还是会失败,错误原因依然是certificate verify failed

查看了certifi的主页
certifi

发现官方也给出了解释:

Unfortunately, old versions of OpenSSL (less than 1.0.2) sometimes
fail to validate certificate chains that use the strong roots. For
this reason, if you fail to validate a certificate using the
certifi.where() mechanism, you can intentionally re-add the 1024-bit
roots back into your bundle by calling certifi.old_where() instead.
This is not recommended in production: if at all possible you should
upgrade to a newer OpenSSL. However, if you have no other option, this
may work for you.

其实大概就是因为openssl的老版本(地域1.0.2)用的校验是strong roots(指的是只信任了少部分ca吗?我也没太懂)。总之,有好几个解决方法:
1、换老版本的certifi来解决(因为老版本的certifi证书比较老,跟老版本的openssl正好合得来),但是这种方法不是非常好,目前看网上用的是certifi==2015.04.28版本,这个版本也没有certifi.old_where(),因为本身就是老的……

2、就用新版本的certifi,但是验证时用certifi.old_where()下面的证书来进行配置

import certifi
AsyncHTTPClient.configure(None, defaults=dict(ca_certs=certifi.old_where()))

3、升级python版本到2.7.9以上,因为这之后,python进行https请求时,不用再通过certifi来配置,而是已经内置了相关的证书。

4、升级openssl到1.0.2及以上。

推荐升级openssl或者Python版本,如果因为环境限制,实在没办法的话用old_where也行。

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
tornado下https配置
越来越多的网站已经支持https,相比于http更安全。尤其有的开发网站只支持https,例如微信公众平台。
<<上一篇
下一篇>>