检测HTTPS证书到期时间
Posted ipyanthony
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了检测HTTPS证书到期时间相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python-devops
# -*- coding: utf-8 -*-
# @Time : 2019-05-31 16:00
# @Author : Anthony.long
# @Site :
# @File : check_https_dated.py
# @Software: PyCharm
import ssl, socket
import requests
from dateutil import parser
import pytz
def get_domain_content(domain):
requests.packages.urllib3.disable_warnings()
url = ‘https://‘ + domain
response = requests.get(url, verify=False).headers
print(response)
def get_my_domain(mydomain):
try:
my_addr = socket.getaddrinfo(mydomain, None)
c = ssl.create_default_context()
s = c.wrap_socket(socket.socket(), server_hostname=mydomain)
s.connect((mydomain, 443))
my_cert = s.getpeercert()
get_my_cert_dated(mydomain, my_cert, my_addr)
except ssl.CertificateError and socket.gaierror as e:
pass
def get_my_cert_dated(domain, certs, my_addr):
cert_beginning_time = parser.parse(certs[‘notBefore‘]).astimezone(pytz.utc)
cert_end_time = parser.parse(certs[‘notAfter‘]).astimezone(pytz.utc)
print(‘域名:(%s) 证书创建时间:%s 证书失效时间: %s IP地址: %s ‘ % (
domain, cert_beginning_time, cert_end_time, my_addr[0][4][0]))
def read_domain_files():
with open(‘/xxxxxxxxx/domain.txt‘, ‘r‘,
encoding="utf-8") as file:
for domain in file:
get_my_domain(domain.strip())
if __name__ == "__main__":
read_domain_files()
以上是关于检测HTTPS证书到期时间的主要内容,如果未能解决你的问题,请参考以下文章