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) sometimesfail to validate certificate chains that use the strong roots. Forthis reason, if you fail to validate a certificate using thecertifi.where() mechanism, you can intentionally re-add the 1024-bitroots back into your bundle by calling certifi.old_where() instead.This is not recommended in production: if at all possible you shouldupgrade to a newer OpenSSL. However, if you have no other option, thismay 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也行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值