import socket
import ssl
from datetime import datetime
def get_ssl_expiry_date(host):
context = ssl.create_default_context()
with context.wrap_socket(socket.socket(), server_hostname=host) as s:
s.connect((host, 443))
cert = s.getpeercert()
return datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
host = 'www.baidu.com'
print(get_ssl_expiry_date(host))
评论区