tlslite-ng

0.8.2 · active · verified Thu Apr 16

tlslite-ng is a pure Python implementation of SSL and TLS protocols, supporting modern standards like TLS 1.3 and various cryptographic algorithms. It aims to provide a flexible and robust solution for secure communication without relying on OpenSSL bindings. The current stable version is 0.8.2, with beta versions (0.9.x) introducing new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a TLS client connection to a public HTTPS server using `tlslite-ng`. It covers creating a socket, wrapping it with `TLSConnection`, performing the handshake with specific `HandshakeSettings`, and then sending/receiving application data. It also includes error handling and proper connection closure.

import socket
from tlslite.api import TLSConnection, HandshakeSettings

# Minimal TLS client example connecting to a public HTTPS server
HOST = 'www.google.com'
PORT = 443

def run_client():
    sock = None
    connection = None
    try:
        # 1. Create a standard socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((HOST, PORT))
        print(f"Connected to {HOST}:{PORT}")

        # 2. Wrap the socket with TLSConnection
        connection = TLSConnection(sock)

        # 3. Perform the TLS handshake as a client
        settings = HandshakeSettings()
        settings.minVersion = (3, 3) # TLS 1.2
        settings.maxVersion = (3, 4) # TLS 1.3

        connection.handshakeClient(
            settings=settings,
            reqCert=True # Request server certificate
        )
        print(f"TLS handshake successful. Protocol: {connection.version_name}")

        # 4. Send and receive application data
        request = b"GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n"
        connection.write(request)
        print("Sent HTTP GET request.")

        response = connection.read()
        print("Received data (first 200 bytes):")
        print(response[:200].decode(errors='ignore'))

    except Exception as e:
        print(f"TLS client failed: {e}")
    finally:
        if connection: # Closes both the TLS connection and underlying socket
            connection.close()
        elif sock:
            sock.close()

if __name__ == '__main__':
    run_client()

view raw JSON →