tornado下https配置

问题背景

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

SSLError

能够用AsyncHTTPClient发送一个简单的https请求git

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

结果出现了以下问题github

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

参考tornado rejects valid SSL certificates安全

这个缘由是由于证书设置不正确,那么咱们能够经过下面的操做给AsyncHTTPClient设置证书。微信

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

可是这个设置之后,会发现虽然不报错了,可是请求仍是会失败,错误缘由依然是certificate verify failed微信公众平台

查看了certifi的主页
certifiless

发现官方也给出了解释:tornado

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.fetch

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

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

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

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

四、升级openssl到1.0.2及以上。

推荐升级openssl或者Python版本,若是由于环境限制,实在没办法的话用old_where也行。

相关文章
相关标签/搜索