温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于python检查SSL证书到期情况代码实例

发布时间:2021-03-11 09:53:04 来源:亿速云 阅读:243 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关基于python检查SSL证书到期情况代码实例,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

代码示例:

# coding: utf-8 
# 查询域名证书到期情况

import re
import time
import subprocess
from datetime import datetime
from io import StringIO

def main(domain):
  f = StringIO()
  comm = f"curl -Ivs https://{domain} --connect-timeout 10"

  result = subprocess.getstatusoutput(comm)
  f.write(result[1])

  m = re.search('start date: (.*?)\n.*?expire date: (.*?)\n.*?common name: (.*?)\n.*?issuer: CN=(.*?)\n', f.getvalue(), re.S)
  start_date = m.group(1)
  expire_date = m.group(2)
  common_name = m.group(3)
  issuer = m.group(4)

  # time 字符串转时间数组
  start_date = time.strptime(start_date, "%b %d %H:%M:%S %Y GMT")
  start_date_st = time.strftime("%Y-%m-%d %H:%M:%S", start_date)
  # datetime 字符串转时间数组
  expire_date = datetime.strptime(expire_date, "%b %d %H:%M:%S %Y GMT")
  expire_date_st = datetime.strftime(expire_date,"%Y-%m-%d %H:%M:%S")

  # 剩余天数
  remaining = (expire_date-datetime.now()).days

  print ('域名:', domain)
  print ('通用名:', common_name)
  print ('开始时间:', start_date_st)
  print ('到期时间:', expire_date_st)
  print (f'剩余时间: {remaining}天')
  print ('颁发机构:', issuer)
  print ('*'*30)

  time.sleep(0.5)

if __name__ == "__main__":
  domains = ['www.baidu.com'] 
  for domain in domains:
    main(domain)

结果示例:

域名: www.baidu.com
通用名: baidu.com
开始时间: 2019-05-09 01:22:02
到期时间: 2020-06-25 05:31:02
剩余时间: 82天
颁发机构: GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
******************************

关于“基于python检查SSL证书到期情况代码实例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI