在python中将服务器证书添加到CA_BUNDLE

问题描述:

我正在使用请求通过https与远程服务器通信。目前我还没有验证SSL证书,我想解决这个问题。

I'm using requests to communicate with remote server over https. At the moment I'm not verifying SSL certificate and I'd like to fix that.

在请求文档中,我发现:

Within requests documentation, I've found that:


您可以通过证书
可信CA来验证路径到CA_BUNDLE文件。此可信CA的列表也可以通过
指定REQUESTS_CA_BUNDLE环境变量。

You can pass verify the path to a CA_BUNDLE file with certificates of trusted CAs. This list of trusted CAs can also be specified through the REQUESTS_CA_BUNDLE environment variable.

我不想使用系统的证书,但要生成我自己的商店。

I don't want to use system's certs, but to generate my own store.

到目前为止,我正在使用ssl.get_server_certificate(addr)获取服务器证书,但我不知道如何创建我的自己的商店并将其添加到那里。

So far I'm grabbing server certificate with ssl.get_server_certificate(addr), but I don't know how to create my own store and add it there.

这实际上是微不足道的...... CA_BUNDLE可以是您附加证书的任何文件,所以你只需将ssl.get_server_certificate()的输出附加到该文件即可。

This is actually trivial... CA_BUNDLE can be any file that you append certificates to, so you can simply append the output of ssl.get_server_certificate() to that file and it works.