Skip to content

SSL certificate expiry watch in Python

Minimal script that checks an SSL certificate and alerts 14 days before expiry.

Recipe

python
import ssl, socket, datetime

def days_left(host: str, port: int = 443) -> int:
    ctx = ssl.create_default_context()
    with socket.create_connection((host, port), timeout=10) as sock:
        with ctx.wrap_socket(sock, server_hostname=host) as ss:
            not_after = ss.getpeercert()["notAfter"]
            exp = datetime.datetime.strptime(not_after, "%b %d %H:%M:%S %Y %Z")
            return (exp - datetime.datetime.utcnow()).days

if days_left("example.com") < 14:
    raise SystemExit("SSL expiring soon")

Same thing in Enterno.io

Enterno SSL check + monitor: automatic email + Telegram at T-14 and T-3 days before expiry, full chain validation (intermediate, OCSP, cipher).

Set up SSL checker → ← All recipes