python获取SSL证书信息

代码有问题,有时间再更新
代码有问题,有时间再更新
代码有问题,有时间再更新

from datetime import datetime
from urllib3.contrib import pyopenssl
def get_ssl_certificate(domain):
    try:
        certificate = pyopenssl.ssl.get_server_certificate((domain, 443))
        data = pyopenssl.OpenSSL.crypto.load_certificate(pyopenssl.OpenSSL.crypto.FILETYPE_PEM, certificate)
        expire_time = datetime.strptime(data.get_notAfter().decode()[0:-1], '%Y%m%d%H%M%S')
        expire_days = (expire_time - datetime.now()).days
        create_time = datetime.strptime(data.get_notBefore().decode()[0:-1], '%Y%m%d%H%M%S')
        # c = data.get_issuer()
        components = data.get_subject().get_components()
        components = {
            i[0].decode('utf-8'): i[1].decode('utf-8')
            for i in components
        }
        return True, 200, {
            **components,
            'expire_time': str(expire_time),
            'expire_days': expire_days,
            'create_time': str(create_time)
        }
    except Exception as e:
        return False, 500, str(e)
if __name__ == '__main__':
    print(get_ssl_certificate('api.uyu.com'))

你可能感兴趣的:(python,后端,ssl,https,服务器,ssl证书,python)