Treat ConnectionRefusedError like SSLCertVerificationError

This commit is contained in:
Peter J. Holzer 2023-10-10 10:28:56 +02:00
parent ad04443928
commit 70b68108c7
1 changed files with 7 additions and 7 deletions

View File

@ -16,15 +16,15 @@ args = ap.parse_args()
now = time.time() now = time.time()
report0 = [] report0 = []
try:
with socket.create_connection((args.hostname, args.port)) as sock: with socket.create_connection((args.hostname, args.port)) as sock:
context = ssl.create_default_context() context = ssl.create_default_context()
try:
with context.wrap_socket(sock, server_hostname=args.hostname) as ssock: with context.wrap_socket(sock, server_hostname=args.hostname) as ssock:
cert = ssock.getpeercert() cert = ssock.getpeercert()
not_after = ssl.cert_time_to_seconds(cert["notAfter"]) not_after = ssl.cert_time_to_seconds(cert["notAfter"])
delta = not_after - now delta = not_after - now
except ssl.SSLCertVerificationError as e: except (ssl.SSLCertVerificationError, ConnectionRefusedError) as e:
print("got error %s; setting delta to 0", e) print("got error %s; setting delta to 0" % e)
delta = 0 delta = 0
report0.append({ "measure": "tls_cert_ttl", "unit": "s", "value": delta }) report0.append({ "measure": "tls_cert_ttl", "unit": "s", "value": delta })