Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4165393
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: Python/Ruby

2018-12-07 15:07:48

python的requests使用 certifi package's certificate管理证书
出现 requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
certifi 使用pem证书,
PEM - Privacy Enhanced Mail,打开看文本格式,以"-----BEGIN..."开头, "-----END..."结尾,内容是BASE64编码.
查看PEM格式证书的信息:openssl x509 -in certificate.pem -text -noout
DER - Distinguished Encoding Rules,打开看是二进制格式,不可读.
查看DER格式证书的信息:openssl x509 -in certificate.der -inform der -text -noout
der转为pem
openssl x509 -inform der -in certificate.cer -out certificate.pem


python 2.7的ca证书位置/usr/local/lib/python2.7/dist-packages/certifi/cacert.pem
使用如下证书可以将ca证书导入


点击(此处)折叠或打开

  1. import certifi
  2. import requests

  3. try:
  4.     print('Checking connection to Slack...')
  5.     test = requests.get('')
  6.     print('Connection to Slack OK.')
  7. except requests.exceptions.SSLError as err:
  8.     print('SSL Error. Adding custom certs to Certifi store...')
  9.     cafile = certifi.where()
  10.     with open('/software/pki/ca.crt', 'rb') as infile:
  11.         customca = infile.read()
  12.     with open(cafile, 'ab') as outfile:
  13.         outfile.write(customca)
  14.     print('That might have worked.')

  15. # Actual app logic here, hopefully without SSL issues.

也可以指定REQUESTS_CA_BUNDLE
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
或者代码


点击(此处)折叠或打开

  1. os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(
  2.     '/etc/ssl/certs/',
  3.     'ca-certificates.crt')
出现hostname 'kennethreitz.com' doesn't match either of '*.herokuapp.com',
把verify=False

点击(此处)折叠或打开

  1. import requests
  2. headers = {'Content-type': 'application/json', 'Accept': 'text/json'}
  3. r = requests.post('',
  4.                  cert=('/software/pki/client.crt', '/software/pki/client.key'), verify=False,
  5.                 headers=headers)
  6. print r.status_code
  7. print r.json()



阅读(29230) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~