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