3

When using the Python Requests library how can I determine which version of TLS was used?

J. Doe
  • 101
  • 1
  • 5

2 Answers2

2

You first need to determine SSL version to get TLS version. TLS stack will use the best version available automatically

import ssl
print ssl.OPENSSL_VERSION
'OpenSSL 1.0.1e-fips 11 Feb 2016'

Also, which version of TLS support you want depends on your SSL version.

Check this nice way to determine TLS version using python

Hari_pb
  • 7,088
  • 3
  • 45
  • 53
  • Thank you for the comment. What if the server has a lower version than my max? Then I can't know until the request is made what version will be used. – J. Doe May 22 '18 at 17:54
  • Documentation of requests shows you can force client to use specific TLS version http://docs.python-requests.org/en/master/user/advanced/#example-specific-ssl-version – Hari_pb May 22 '18 at 17:56
  • This is an option, but I am currently trying to see if I can just log it. – J. Doe May 22 '18 at 18:06
2

According to this answer there is no way to get the socket information (which holds the TLS information as far as I understand) after a requests request is executed (unless it is a streaming request).

As Harry_pb points out, your SSL version and the server's TLS version determines the TLS version used in the connection.

The socket library docs shows how to get a socket's TLS version:

import socket
import ssl

hostname = 'www.python.org'
context = ssl.create_default_context()

with socket.create_connection((hostname, 443)) as sock:
    with context.wrap_socket(sock, server_hostname=hostname) as ssock:
        print(ssock.version())
vekerdyb
  • 1,213
  • 12
  • 26