我在公司网络上,我需要使用证书才能访问某些页面。我尝试在网上四处寻找可以检索 HTTPS 网页并允许我指定要使用的证书的模块。我有这段代码:
import requests
page = requests.get("https://k9ballistics.com/",'lxml')
thing = page.content
thing = thing.split('\n')
for m in thing:
if '<tr>' in m:
print m
这适用于检索普通的 HTTPS 页面,但是当我尝试访问我们的页面时它会抛出此错误:
requests.exceptions.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)
我希望找到一种方法来使用 Python 已经附带的模块,而不是为了可移植性而依赖 pip 安装包。
我在 Windows 上,但我的证书来 self 的 linux 工作站,位于我想指向的文件夹中,并且在 Windows 上也有 Ubuntu Bash。
最佳答案
您可以通过验证路径到 CA_BUNDLE
文件或具有可信 CAs
证书的目录:
requests.get('https://eg.com', verify='/path/to/certfile.pem')
或坚持:
s = requests.Session()
s.verify = '/path/to/certfile.pem'
您也可以通过 verify=False
忽略验证 SSL 证书。
看看SSL Cert Verification以查看更多详细信息。
关于python - 在 Python2.7.* 中使用 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43648096/