巨 Python

import requests
import ssl
from datetime import datetime

def check_ssl_certificate(url):
    try:
        # SSL証明書の取得
        response = requests.get(url, verify=True)
        cert = ssl.get_server_certificate((url, 443))
        
        # 証明書情報を解析
        x509 = ssl.PEM_cert_to_DER_cert(cert)
        x509 = ssl.DER_cert_to_PEM_cert(x509)
        
        # 有効期限を取得
        x509_info = ssl._ssl._test_decode_cert(x509)
        valid_from = datetime.utcfromtimestamp(x509_info['notBefore'])
        valid_to = datetime.utcfromtimestamp(x509_info['notAfter'])
        
        # 現在の日時を取得
        current_time = datetime.utcnow()
        
        # 有効期限の比較
        if current_time >= valid_from and current_time <= valid_to:
            print("valid")
        else:
            print("invalid")
        
    except requests.exceptions.SSLError as e:
        print("can't receive the sslcertificate")

# チェックするURLを指定
url = 'https://www.google.com'

# SSL証明書の有効性を確認
check_ssl_certificate(url)

错误语句

Traceback (most recent call last):
  File "c:\Users\uc100\Programing\Python\Codingspace\everything\check.py", line 36, in <module>
    check_ssl_certificate(url)
  File "c:\Users\uc100\Programing\Python\Codingspace\everything\check.py", line 9, in check_ssl_certificate
    cert = ssl.get_server_certificate((url, 443))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\uc100\Programing\Python\Python311\Lib\ssl.py", line 1527, in get_server_certificate
    with create_connection(addr, timeout=timeout) as sock:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\uc100\Programing\Python\Python311\Lib\socket.py", line 827, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\uc100\Programing\Python\Python311\Lib\socket.py", line 962, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed

I want to check if the SSL certificate for a certain link is valid by 巨 Python .However, an 错误语句 like the one shown above is output.I have no idea what the error message is saying, so please tell me if you know what it means.please.

推荐答案

要判断过期日期,您可以执行以下操作:

import datetime
import socket
import ssl
def get_date_before_expired(hostname, port = '443'):
    """
    Get expired date of ssl cert
    """    
    context = ssl.create_default_context()
    with socket.create_connection((hostname, port)) as sock:
        with context.wrap_socket(sock, server_hostname=hostname) as ssock:
            cert_info = ssock.getpeercert()
            expiry_date = datetime.datetime.strptime(cert_info['notAfter'], '%b %d %H:%M:%S %Y %Z')
            print(expiry_date)
            return expiry_date
if __name__ == '__main__':
    get_date_before_expired('www.google.com')

Python相关问答推荐

Django文件上传不起作用:文件未出现在媒体目录或数据库中

Docker-compose:为不同项目创建相同的容器

将嵌套列表的字典转换为数据框中的行

如何自动抓取以下CSV

根据条件将新值添加到下面的行或下面新创建的行中

Python daskValue错误:无法识别的区块管理器dask -必须是以下之一:[]

'discord.ext. commanders.cog没有属性监听器'

rame中不兼容的d类型

需要计算60,000个坐标之间的距离

如何在虚拟Python环境中运行Python程序?

在Mac上安装ipython

如果值发生变化,则列上的极性累积和

未知依赖项pin—1阻止conda安装""

将pandas导出到CSV数据,但在此之前,将日期按最小到最大排序

在pandas数据框中计算相对体积比指标,并添加指标值作为新列

在Python中计算连续天数

matplotlib图中的复杂箭头形状

交替字符串位置的正则表达式

在Python中控制列表中的数据步长

Js的查询结果可以在PC Chrome上显示,但不能在Android Chrome、OPERA和EDGE上显示,而两者都可以在Firefox上运行