Reference library

Python Code Samples

Easy snippets you can copy, study, and run in the browser editor.

4 matches
Auth & security at scale easy

Enforce TLS 1.2 Minimum in Python

Create an SSL context with a minimum TLS version of 1.2 to enforce secure connections.

tls ssl security
Python
import ssl

def get_min_tls_version():
    context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    context.minimum_version = ssl.TLSVersion.TLSv1_2
    return context.minimum_version

if __name__ == "__main__":
    min_version = get_min_tls_version()
    print(f"Minimum TLS version set to: {min_version.name} (value: {mi…
13 0 Open
Auth & security at scale easy

How to Check Negotiated Cipher Suite in Python

Connect to a TLS server with Python's ssl module and print the negotiated protocol version and cipher suite details.

tls ssl security
Python
import ssl
import socket

def get_cipher_suites(hostname, port=443):
    context = ssl.create_default_context()
    context.set_ciphers("DEFAULT:@SECLEVEL=2")
    
    with socket.create_connection((hostname, port), timeout=5) as sock:
        with context.wrap_socket(sock, server_hostname=hostname) as ssock:
        …
17 0 Open
Auth & security at scale easy

How to Mock a TLS Certificate Rotation Schedule in Python

Simulate a TLS certificate rotation schedule with a Python class that tracks last and next rotation dates and decides when to rotate.

tls certificates rotation
Python
import datetime
import random
import time


class CertRotator:
    def __init__(self, cert_name, rotation_days=30):
        self.cert_name = cert_name
        self.rotation_days = rotation_days
        self.last_rotated = datetime.date.today() - datetime.timedelta(days=random.randint(10, 25))
        self.next_rotatio…
16 0 Open
Production deployment patterns easy

How to Mock an Ingress TLS Certificate Manager in Python

Build a mock TLS certificate manager for ingress that issues, checks, and renews certificates with expiry tracking — useful for testing deployment workflows before touching real infrastructure.

tls certificates ingress
Python
import ssl
import socket
from datetime import datetime, timedelta


class TLSCertManager:
    def __init__(self, hostname):
        self.hostname = hostname
        self.certificates = {}

    def request_certificate(self, domain, days_valid=90):
        """Mock a certificate issuance request that stores a cert with e…
15 0 Open

Browse by section

Each section groups closely related Python snippets.

Guide: free Python code samples library

Copy-ready Python snippets for learners and developers

PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.

How to use this library

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. Run it in the IDE, tweak values, then take a related quiz or tutorial lesson

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.