小编Bal*_*ngh的帖子

如何获取 SSL 证书以查看它是否已过期

我没有从这段代码中得到任何输出。好消息是我没有得到任何错误。请告诉我我做错了什么。这是我的代码或任何其他方法来查找 ssl 证书的过期日期(使用仅限 Python)

import datetime
import logging
import socket
import ssl

YOUR_DOMAIN = 'google.com'
WARNING_BUFFER = 14

logger = logging.getLogger()
logger.setLevel(logging.INFO)

ssl_date_fmt = r'%b %d %H:%M:%S %Y %Z'

class AlreadyExpired(Exception):
    pass

def ssl_expires_in(hostname, buffer_days=14):
    """Gets the SSL cert from a given hostname and checks if it expires within buffer_days"""
    context = ssl.create_default_context()
    conn = context.wrap_socket(
        socket.socket(socket.AF_INET),
        server_hostname=hostname,

    )
    # 3 second timeout because Lambda has runtime limitations
    conn.settimeout(3.0)
    conn.connect((hostname, 443))
    ssl_info = conn.getpeercert()
    expires = datetime.datetime.strptime(ssl_info['notAfter'], ssl_date_fmt)

    # if …
Run Code Online (Sandbox Code Playgroud)

python ssl plugins

4
推荐指数
1
解决办法
6448
查看次数

标签 统计

plugins ×1

python ×1

ssl ×1