在Python2.7中使用SSL证书。*

时间:2017-04-27 03:21:24

标签: 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上,但是我的Linux工作站的证书来自我想指向的文件夹,并且在Windows上也有Ubuntu Bash。

1 个答案:

答案 0 :(得分:2)

您可以使用受信任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以查看更多详情。