-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_ssl.py
More file actions
35 lines (27 loc) · 1.57 KB
/
check_ssl.py
File metadata and controls
35 lines (27 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Код для проверки срока действия SSL-сертификата на Python
# Для проверки SSL-сертификата в коде используется библиотека ssl, для работы с сетевыми соединениями
# и сокетами используется библиотека socket.
import ssl
import socket
from datetime import datetime, timezone
def check_ssl(hostname, port=443):
context = ssl.create_default_context()
try:
with socket.create_connection((hostname, port)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
cert = ssock.getpeercert()
print(f"Сертификат для: {hostname}")
print(f"Организация: {cert.get('issuer')}")
print(f"Выдан: {cert.get('notBefore')}")
print(f"Действителен до: {cert.get('notAfter')}")
# Проверка срока действия с timezone-aware объектом
not_after = datetime.strptime(cert['notAfter'], '%b %d %H:%M:%S %Y %Z')
not_after = not_after.replace(tzinfo=timezone.utc)
if not_after < datetime.now(timezone.utc):
print("Сертификат просрочен!")
else:
print("Сертификат действителен")
except Exception as e:
print(f"Ошибка при проверке SSL: {e}")
if __name__ == "__main__":
check_ssl("example.com")