From 7edd241059c3047082cc635a23b073db0bee32f8 Mon Sep 17 00:00:00 2001 From: speedmann Date: Fri, 7 Dec 2018 11:08:41 +0100 Subject: [PATCH] Automatically detect if ipv4/ipv6 is used for cert_expiry (#18916) * Automatically detect if ipv4/ipv6 is used for cert_expiry Fixes #18818 Python sockets use ipv4 per default. If the domain which should be checked only has an ipv6 record, socket creation errors out with `[Errno -2] Name or service not known` This fix tries to guess the protocol family and creates the socket with the correct family type * Fix line length violation --- homeassistant/components/sensor/cert_expiry.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/cert_expiry.py b/homeassistant/components/sensor/cert_expiry.py index df48ebbf41c..a04a631f2e9 100644 --- a/homeassistant/components/sensor/cert_expiry.py +++ b/homeassistant/components/sensor/cert_expiry.py @@ -85,8 +85,10 @@ class SSLCertificate(Entity): """Fetch the certificate information.""" try: ctx = ssl.create_default_context() + host_info = socket.getaddrinfo(self.server_name, self.server_port) + family = host_info[0][0] sock = ctx.wrap_socket( - socket.socket(), server_hostname=self.server_name) + socket.socket(family=family), server_hostname=self.server_name) sock.settimeout(TIMEOUT) sock.connect((self.server_name, self.server_port)) except socket.gaierror: