128 Star 565 Fork 217

mktime / python-learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
check_https_domain_exp_date.py 965 Bytes
一键复制 编辑 原始数据 按行查看 历史
mktime 提交于 2022-11-18 21:23 . 检查https证书是否过期
import ssl
import OpenSSL
import datetime
from pytz import timezone
'''
date: 2022-11-18 21:22:14
参考: https://knktc.com/2021/06/20/use-python-to-check-ssl-expiry-date/
安装依赖库: pip install pyOpenSSL
'''
SRC_TZ = 'UTC'
DST_TZ = 'Asia/Shanghai'
def load_ssl_date(dt_string, pattern='%Y%m%d%H%M%SZ'):
""" convert ssl date from string to datetime obj """
src_tz = timezone(SRC_TZ)
dst_tz = timezone(DST_TZ)
dt = src_tz.localize(datetime.datetime.strptime(dt_string, pattern))
return dt.astimezone(tz=dst_tz)
def get_domain_exp_date(host, port=443):
cert = ssl.get_server_certificate((host, port))
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
utctime = x509.get_notAfter().decode()
exp_date = load_ssl_date(utctime)
return exp_date
def main():
domain = 'www.baidu.com'
exp_date = get_domain_exp_date(domain)
print(domain, exp_date)
if __name__ == '__main__':
main()
Python
1
https://gitee.com/mktime/python-learn.git
git@gitee.com:mktime/python-learn.git
mktime
python-learn
python-learn
master

搜索帮助