在 Python 2.6 中发送 TLS 1.2 请求 [英] Sending TLS 1.2 request in Python 2.6

查看:157
本文介绍了在 Python 2.6 中发送 TLS 1.2 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am stuck using Python 2.6 and I need to send a post request using TLS 1.2. Does Python 2.6's requests library support TLS 1.2? How do I ensure/verify that the request is made via TLS1.2 and not some other version?

示例请求是

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

我在论坛的某个地方知道我们需要编译 pyOpenSSL 来支持这一点.有没有更简单的方法?

Somewhere on the forum I came to know that we need to compile pyOpenSSL to support this. Is there an easier way?

推荐答案

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

The ssl module in Python 2.6 supports up to TLS 1.0 only. If you do not wish to introduce additional dependencies (such as pyOpenSSL as you suggest) you will need to upgrade to Python 2.7 or 3.x to get support for newer versions of TLS.

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

To force a particular version of TLS in Python 2.7.9 or later, construct an SSLContext with the appropriate PROTOCOL_* constant. You can then use it with any API that lets you provide your own SSLContext.

import ssl
import urllib2

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

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

To use a particular protocol version or higher (including future versions), use ssl.PROTOCOL_SSLv23 and then disable the protocol versions you do not want to use:

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 以强制特定协议版本,根据文档 似乎没有办法做到这一点请参阅文档中的以下示例.

As for using a custom SSLContext with Requests in order to force a particular protocol version, according to the documentation there does not seem to be a way to do this, see the following example from the docs.

这篇关于在 Python 2.6 中发送 TLS 1.2 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆