Python Requests - Use TLS v.1_3

跟風遠走 提交于 2021-01-02 20:29:13

问题


I am trying to make a request in Python using TLS 1.3 as it is what the site accepts. I use following code:

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
from requests.packages.urllib3.util import ssl_

CIPHERS = (
    'AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:AES256-SHA'
)


class TlsAdapter(HTTPAdapter):

    def __init__(self, ssl_options=0, **kwargs):
        self.ssl_options = ssl_options
        super(TlsAdapter, self).__init__(**kwargs)

    def init_poolmanager(self, *pool_args, **pool_kwargs):
        ctx = ssl_.create_urllib3_context(ciphers=CIPHERS, cert_reqs=ssl.CERT_REQUIRED, options=self.ssl_options)
        self.poolmanager = PoolManager(*pool_args,
                                       ssl_context=ctx,
                                       **pool_kwargs)


sess = requests.session()
adp = TlsAdapter(ssl.OP_NO_TLSv1_3 | ssl.OP_NO_TLSv1_3)
sess.mount("https://", adapter)

Everything looks fine until here. I started using it to send requests ecc but of pure curiosity I checked deeper and used Wireshark. Here it says the requests go over 1.2 and not 1.3.

Anyone could help me actually to enforce 1.3 and not 1.2? It would be very appreciated. Thanks.

来源:https://stackoverflow.com/questions/57599480/python-requests-use-tls-v-1-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!