Free cookie consent management tool by TermsFeed The version() method of SSLSocket class from the Python ssl module | Pythontic.com

The version() method of SSLSocket class from the Python ssl module

Method Name:

SSLSocket.version

Method Signature:

SSLSocket.version()

Parameters:

None

Return Value:

The version of the SSL protocol obtained during the SSL handshake. The value is from the set of protocol values {"SSLv2", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"} 

Overview:

  • When an SSL connection is established between two machines in the network, several parameters are established like
    • Protocol version
    • Cipher Suite
    • Compression Method

which will be used for the communication.

  • The method SSLSocket.version() returns the version of the SSL protocol to be used in the communication between two SSL sockets.

Example:

# Example Python program that prints the SSL
# protocol version used on an established SSL connection

import ssl
import socket
import os
import platform

# Create an SSL context
context = ssl.create_default_context()
 
context.verify_mode     = ssl.CERT_REQUIRED
context.check_hostname  = True
context.load_default_certs()
 
# Check for OS X platform
if platform.system().lower() == 'darwin':
    import certifi
    context.load_verify_locations(
        cafile=os.path.relpath(certifi.where()),
        capath=None,
        cadata=None)

# Create a streaming socket
con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   

# Create a secure socket
secure_con          = context.wrap_socket(con, server_hostname="example.org")
protocol_version    = secure_con.version()

print("The SSL protocol version before calling SSLSocket.connect() is:")
print(protocol_version)

# Connect to host
secure_con.connect(("example.org", 443))
 
# Find the SSL protocol version obtained after SSL handshake
protocol_version = secure_con.version()
print("The SSL protocol version after calling SSLSocket.connect() is:")
print(protocol_version)

 

Output:

The SSL protocol version before calling SSLSocket.connect() is:
None
The SSL protocol version after calling SSLSocket.connect() is:
TLSv1.3

 


Copyright 2025 © pythontic.com