Ai
1 Star 0 Fork 265

xuyan11/python-learn

forked from mktime/python-learn 
Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
check_https_domain_exp_date.py 965 Bytes
Copy Edit Raw Blame History
mktime authored 2022-11-18 21:23 +08:00 . 检查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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/xy11/python-learn.git
git@gitee.com:xy11/python-learn.git
xy11
python-learn
python-learn
master

Search