2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

証明書期限切れまであと何日かチェックする

Last updated at Posted at 2020-01-14
ssl_expire.py
import socket
import ssl
import OpenSSL

def get_server_certificate(hostname):
    context = ssl.create_default_context()
    with socket.create_connection((hostname, 443)) as sock:
        with context.wrap_socket(sock, server_hostname=hostname) as sslsock:
            der_cert = sslsock.getpeercert(True)
            return ssl.DER_cert_to_PEM_cert(der_cert)

cert = get_server_certificate('qiita.com')
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert.encode('utf-8'))

import datetime
from datetime import datetime as dt
not_before = dt.strptime(str(x509.get_notBefore())[2:16],'%Y%m%d%H%M%S') + datetime.timedelta(hours=9)
not_after  = dt.strptime(str(x509.get_notAfter())[2:16],'%Y%m%d%H%M%S')  + datetime.timedelta(hours=9)
print(not_after)
aaa = not_after - dt.now()
print(aaa)
$ python3 ssl_expire.py
2020-04-30 21:00:00
106 days, 19:33:22.726942

qiita.comの証明書はあと106日後に切れる

参考URL

ssl.get_server_certificateで証明書を取得できない場合の対処 - Qiita

ssl --- ソケットオブジェクトに対する TLS/SSL ラッパー — Python 3.8.1 ドキュメント

Python サイトのSSL証明書の有効期限を取得する - Symfoware

SSL証明書の期限取得 - Qiita

x509証明書 - 雑多なメモ置き場

無題メモランダム: PythonでX日前/X日後を求める方法

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?