python use tls 1.2

Python Usage of TLS 1.2

Transport Layer Security (TLS) 1.2 is a protocol used to secure data transmission over the internet. Python, being a popular language for web development and network programming, provides ways to use TLS 1.2 in its code.

Using Requests Library

One way to use TLS 1.2 in Python is through the Requests library which is commonly used for making HTTP requests. By default, Requests uses the highest available TLS version supported by the system but we can specify the version we want to use.

      import requests

      url = ""
      response = requests.get(url, verify=True, tls_version=1.2)


In the code above, we imported the Requests library and specified the URL we want to make a GET request to. We then used the requests.get() method to make the request with the verify parameter set to true to verify the SSL certificate of the website. We also specified the TLS version we want to use by setting the tls_version parameter to 1.2.

Using OpenSSL Library

Another way to use TLS 1.2 in Python is through the OpenSSL library which provides cryptographic functions for secure communication. We can use the ssl module in Python which is part of the standard library to create an SSL context with TLS 1.2 enabled.

      import socket
      import ssl

      HOST = ''
      PORT = 443

      context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
      conn = socket.create_connection((HOST, PORT))
      conn_ssl = context.wrap_socket(conn, server_hostname=HOST)

      conn_ssl.send(b'GET / HTTP/1.1\r\nHost:\r\n\r\n')
      response = conn_ssl.recv()


In the code above, we first imported the socket and ssl modules. We then specified the HOST and PORT we want to connect to. We created an SSL context with the PROTOCOL_TLSv1_2 parameter which enables TLS 1.2. We then created a socket connection to the host and wrapped it with the SSL context. We sent a GET request to the host and received the response which we printed.