The compression() method of sslsocket class in Python

The method compression() returns the compression method used in the TLS (also still called as SSL) communication. The Python example connects to a server and prints the compression method used in the TLS communication.

Sslsocket Updated 5 Apr 2020 1 min read

Method Name

compression

Method Signature

compression()

Return Value

  • A python string specifying the compression method used.
  • If no compression is used the method returns None.

Overview

  • The method compression() returns the compression method used in the secure communication if any.

Example

python
# Example Python program that retrieves the

            # compression level used in the TLS communication

            import socket

            import ssl

            import platform

# Context

            ctx               = ssl.create_default_context();

            ctx.verify_mode   = ssl.CERT_REQUIRED

# Check for OS X platform

            if platform.system().lower() == 'darwin':

                import certifi

                import os

    # Load CA certificates used for validating the peer's certificate

                ctx.load_verify_locations(

                    cafile=os.path.relpath(certifi.where()),

                    capath=None,

                    cadata=None)

# Connect to the server through an instance of an SSLSocket class

            skt   = socket.socket()

            sec_skt  = ctx.wrap_socket(skt, server_hostname="example.org")

# Make a connection to the server

            sec_skt.connect(("example.com", 443))

            comp = sec_skt.compression()

            print("Compression used:")

            print(comp)

Output

Output
Compression used:

None