得之我幸 失之我命

when someone abandons you,it is him that gets loss because he lost someone who truly loves him but you just lost one who doesn’t love you.

requests 和 ca 证书的故事

受制于人最大的坏处就是处处落后一步,凡事都只能见招拆招

这不,实验室订阅系统更新了,更换了 url 不说,还改成了 https 协议,免不了增加证书,旧时 requests 访问直接不可用

趁这个机会,也复习了一下 requests 关于 ca 证书的一个使用

背景知识:当前 requests 会默认使用 certifi 包里的证书

1
2
3
4
5
6
7
8
# utils.py
from . import certs
DEFAULT_CA_BUNDLE_PATH = certs.where()

# certs.py
from certifi import where
if __name__ == '__main__':
print(where())

问题 1: certifi 包的证书路径在哪儿?

1
2
import certifi
certifi.where()

问题 2: requests 包的默认证书路径是哪儿?

1
2
from requests.utils import DEFAULT_CA_BUNDLE_PATH
DEFAULT_CA_BUNDLE_PATH

问题 3: 将自有证书更新到默认证书文件,以便 requests 调用?

1
2
3
4
5
$ cp /path/to/crt_name /usr/local/share/ca-certificates/
$ update-ca-certificates # debian 系用这个命令

# 还能更简单
$ cat /path/to/crt_name >> DEFAULT_CA_BUNDLE_PATH

问题 4: 想从默认证书文件中删除自有证书?

1
2
$ rm -f /usr/local/share/ca-certificates/crt_name
$ update-ca-certificates

其实即使不将自有证书加入到系统证书文件中,也是可以让 requests 调用的

1
requests.get(url=url, verify='/path/to/crt_name')

be slow to promise and quick to perform.