python - 在 Python 2.6 中发送 TLS 1.2 请求

标签 python ssl request python-requests python-2.6

我无法使用 Python 2.6,我需要使用 TLS 1.2 发送发布请求。 Python 2.6 的 requests 库是否支持 TLS 1.2?如何确保/验证请求是通过 TLS1.2 而不是其他版本发出的?

示例请求是

r=requests.post(url,data=payload,verify=False)

在论坛的某个地方,我开始知道我们需要编译 pyOpenSSL 来支持它。有没有更简单的方法?

最佳答案

Python 2.6 中的 ssl 模块仅支持 TLS 1.0。如果您不想引入额外的依赖项(例如您建议的 pyOpenSSL),您将需要升级到 Python 2.7 或 3.x 以获得对更新版本的 TLS 的支持。

要在 Python 2.7.9 或更高版本 中强制使用特定版本的 TLS,construct an SSLContext与适当的 PROTOCOL_* constant .然后,您可以将它与允许您提供自己的 SSLContext 的任何 API 一起使用。

import ssl
import urllib2

ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
# set other SSLContext options you might need
response = urllib2.urlopen(url, context=ctx)

要使用特定协议(protocol)版本或更高(包括 future 版本),请使用ssl.PROTOCOL_SSLv23,然后禁用您不想使用的协议(protocol)版本:

ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

# allow TLS 1.2 and later
ctx.options |= ssl.OP_NO_SSLv2
ctx.options |= ssl.OP_NO_SSLv3
ctx.options |= ssl.OP_NO_TLSv1
ctx.options |= ssl.OP_NO_TLSv1_1

关于将自定义 SSLContext 与 Requests 一起使用以强制执行特定协议(protocol)版本, according to the documentation似乎没有办法做到这一点see the following example from the docs .

关于python - 在 Python 2.6 中发送 TLS 1.2 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29153271/

相关文章:

python - 使用类装饰器,如何在不重新定义类的情况下重写方法?

python - 我可以使用什么正则表达式从该字符串中捕获组?

python - 通过已部署的 App Engine 应用连接到 Cloud SQL(不同区域)

javascript - 使用 NodeJS 以 C# 格式访问服务器的 API session 时出现问题

javascript - Node.js 如何将冒号分隔的键值对序列化为 JSON

ElasticSearch:为什么query_string不区分大小写而通配符区分大小写?

python - 如何使用 Tesseract 提高图像质量以从图像中提取文本

c# - 通过 SSL 连接到 IRC

SSL 证书也在暂存中

.net - 使用自签名 SSL 证书的 SQL Server 加密。从 ASP.NET 3.5 查询