巨 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.