The version of TLS used in the requests module of python depends on the related module.
Reference: What is the default TLS version of the python requests module
Here, I will write a method to specify the TLS version of the requests module, for example, if you want to use TLS1.0.
Create a subclass of HTTPAdapter as shown below, specify the TLS version, and use the requests module. The following is an example of specifying TLS1.0.
# -*- coding:utf-8 -*-
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl
#Subclass of HTTPAdapter
class MyAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize,
block=block,
ssl_version=ssl.PROTOCOL_TLSv1)
if __name__ == '__main__':
import requests
url = 'https://xxxx.com'
s = requests.Session()
s.mount('https://', MyAdapter())
response = s.get(url)
print(response)
The description method of each version is as follows.
ver | notation |
---|---|
TLS1.0 | ssl.PROTOCOL_TLSv1 |
TLS1.1 | ssl.PROTOCOL_TLSv1_1 |
TLS1.2 | ssl.PROTOCOL_TLSv1_2 |
[Related article] What is the default TLS version of the python requests module Note on how to specify the TLS version with cURL, OpenSSL command
[reference] Python requests SSLError: EOF occurred in violation of protocol
Recommended Posts