2025-05-05 00:47:57 +03:00

19 lines
704 B
Python

import ssl
import socket
from datetime import datetime, timezone
from typing import Tuple
def get_cert_expiry_utc(host: str, port: int = 443) -> datetime:
"""
Устанавливает TLS-соединение и возвращает дату окончания сертификата
в UTC (timezone-aware).
"""
ctx = ssl.create_default_context()
with ctx.wrap_socket(socket.socket(), server_hostname=host) as sock:
sock.settimeout(5)
sock.connect((host, port))
cert = sock.getpeercert()
exp_str = cert['notAfter'] # 'Jun 15 12:00:00 2025 GMT'
dt = datetime.strptime(exp_str, '%b %d %H:%M:%S %Y %Z')
return dt.replace(tzinfo=timezone.utc)