python - 在 Python2.7.* 中使用 SSL 证书

标签 python ssl

我在公司网络上,我需要使用证书才能访问某些页面。我尝试在网上四处寻找可以检索 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/

相关文章:

Magento Ajax 登录 - 通过 SSL

python - 如果只有两位数字,则将 "0"添加到字符串中的数字

python - 通过将列转换为索引来 reshape 数据框

python - Dask SVD 计算和中间值的重用

ssl - TLS:硬禁用密码与不列出密码

c# - Windows 证书存储

qt - QNetworkAccessManager 请求失败(CA 签名证书)

python - OpenCV 的 Python ORB 模块的掩码格式是什么?

Python-删除列表中包含其他单词的所有单词

python请求ssl错误550