python - 旧 python 版本的 SSL 上下文

标签 python sockets ssl https sslcontext

我有如下代码:

headers = {'content-type': 'ContentType.APPLICATION_XML'}
uri = "www.client.url.com/hit-here/"
clientCert = "path/to/cert/abc.crt"
clientKey = "path/to/key/abc.key"
PROTOCOL = ssl.PROTOCOL_TLSv1
context = ssl.SSLContext(PROTOCOL)
context.load_default_certs()
context.load_cert_chain(clientCert, clientKey)
conn = httplib.HTTPSConnection(uri, some_port, context=context)

我不是真正的网络程序员,所以我在谷歌上搜索了握手连接,发现 ssl.SSLContext(PROTOCOL) 是所需的函数,代码工作正常。

然后我遇到了障碍,我的本地版本是 2.7.10,但所有生产机器都有 2.7.3,所以不支持 SSLContext 并且升级 python 版本不是一个选项/在控制。

我尝试阅读 ssl — SSL wrapper for socket objects但无法理解。

我尝试了什么(徒劳):

s_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = ssl.wrap_socket(s_, keyfile=clientKey, certfile=clientCert, cert_reqs=ssl.CERT_REQUIRED)
new_conn = s.connect((uri, some_port))

但返回:

SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)')

问题 - 如何在旧版本上生成 SSL 上下文以获得安全的 https 连接?

最佳答案

您必须指定 ca_certs 文件(应该指向信任库)

关于python - 旧 python 版本的 SSL 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36026846/

相关文章:

python Flask 返回重新启动之前的执行情况

python - 跨多个测试脚本模拟 Python 模块

Python 3 [类型错误 : 'str' object cannot be interpreted as an integer] when working with sockets

java - com.sun.mail.smtp.SMTPSendFailedException : 553 Sorry, 您的信封发件人在我的死信列表中

ssl - 扭曲的 SMTP 服务器,添加 SSL 支持,2 个工厂?

php - BeautifulSoup 和 php/html 文件

python - Django - 开放时间表单集

Java:套接字还是 RMI?

c 套接字发送文件大小,然后发送文件

ssl - tomcat 8.5 Centos 7 Nginx - 如何更改 War Web 应用程序的起始文件夹