How to specify TLS version in python requests

Overview

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.

environment

Create a subclass of HTTPAdapter

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

How to specify TLS version in python requests
How to check opencv version in python
How to change Python version
How to develop in Python
[Python] How to do PCA in Python
How to collect images in Python
How to use Requests (Python Library)
How to use SQLite in Python
How to get the Python version
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
A story about how to specify a relative path in python.
How to specify Cache-Control for blob storage in Azure Storage in Python
Data science companion in python, how to specify elements in pandas
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to enable SSL (TLS) in Apache
How to specify non-check target in Flake8
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
[Beginner memo] How to specify the library reading path in Python
How to use the C library in Python
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
[2020 version] How to install Python3 on AWS EC2
How to install Python
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
Summary of how to import files in Python 3
Let's understand how to pass arguments (Python version)
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
How to install python
Summary of how to use MNIST in Python
How to specify attributes with Mock of python
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python