Fetch the repository succeeded.
This action will force synchronization from mktime/python-learn, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。