Certificate Expiry Calculator
Calculate days until SSL/TLS certificate expiry and track multiple certificates.
Frequently Asked Questions
Code Implementation
import ssl
import socket
from datetime import datetime, timezone
def get_cert_expiry(hostname: str, port: int = 443) -> dict:
"""Fetch TLS certificate expiry info for a hostname."""
context = ssl.create_default_context()
with socket.create_connection((hostname, port), timeout=5) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
cert = ssock.getpeercert()
not_after = datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
not_after = not_after.replace(tzinfo=timezone.utc)
now = datetime.now(timezone.utc)
days_remaining = (not_after - now).days
return {
"hostname": hostname,
"expires": not_after.strftime('%Y-%m-%d'),
"days_remaining": days_remaining,
"status": "expired" if days_remaining < 0
else "critical" if days_remaining < 14
else "warning" if days_remaining < 30
else "good",
"subject": dict(x[0] for x in cert.get('subject', [])),
"issuer": dict(x[0] for x in cert.get('issuer', [])),
}
# Example
result = get_cert_expiry("example.com")
print(f"Expires: {result['expires']}")
print(f"Days remaining: {result['days_remaining']}")
print(f"Status: {result['status']}")Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.