1 Star 0 Fork 237

Gavin/python-learn

forked from 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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/zombiely/python-learn.git
git@gitee.com:zombiely/python-learn.git
zombiely
python-learn
python-learn
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385