情况:
目标站点(例如,pre-prod URL,比如 https://my-pre-prod-site.com/login)正在使用自签名证书。
从浏览器中,可以通过 https 访问该站点而没有任何问题(通过将证书添加到浏览器中的信任库来抑制自签名证书警告)
问题说明:
使用请求对目标站点进行 get 调用的简单 python 脚本在不同情况下失败,并出现以下错误:
requests.exceptions.SSLError: [Errno 0] _ssl.c:344: error:00000000:lib(0):func(0):reason(0)
或者
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590) The simple script used (on the python prompt) is :
import requests
res = requests.get('https://my-pre-prod-site.com/login')
**已经尝试过的东西**
res = requests.get('https://my-pre-prod-site.com/login', verify = os.path.join(os.getcwd(),'test.pem')
其中 test.pem 是通过按顺序连接以下命令的输出创建的 pem 文件:openssl rsa -in ~/Desktop/CertPath/private.key -check
和
openssl x509 -pubkey -noout -in ~/Desktop/CertPath/certificate.pem
该脚本从 ~/Desktop/CertPath 运行,因此 getcwd() 提供了正确的证书路径。
如果有帮助,环境详细信息
操作系统 - ElCapitan Mac
请求 - 2.9.0
Python - 2.7.10
Python 正在使用 OpenSSL - 'OpenSSL 0.9.8zg 2015 年 7 月 14 日'
注 - openssl 版本似乎不是问题。因为即使使用更新版本的 openssl,错误也是一样的 - 在 Ubuntu 上测试过
使用使用 Openssl 1.x 的 Python 2.6
最佳答案
这个问题很老,但万一有人想知道这里。
您将私钥和公钥放在 test.pem 中。这是错误的。验证参数需要的是它可以信任的证书。
res = requests.get('https://my-pre-prod-site.com/login', verify = os.path.join(os.getcwd(),'test.pem')
test.pem 应该包含所有可信证书的列表。但是您在 test.pem 中提供的是您的公钥和私钥。你是 ~/Desktop/CertPath/certificate.pem 文件本身应该进入它。
尝试这个:
res = requests.get('https://my-pre-prod-site.com/login', verify = '~/Desktop/CertPath/certificate.pem')
关于python-2.7 - 在python中使用带有请求的自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34767484/