diff --git a/fluent/sender.py b/fluent/sender.py index 6762856..aaf6e49 100644 --- a/fluent/sender.py +++ b/fluent/sender.py @@ -122,6 +122,13 @@ def close(self): self._close() self.pendings = None + def _is_ipv4_host(self): + try: + socket.getaddrinfo(self.host, None, socket.AF_INET) + return True + except socket.error: + return False + def _make_packet(self, label, timestamp, data): if label: tag = '.'.join((self.tag, label)) @@ -203,7 +210,12 @@ def _reconnect(self): sock.settimeout(self.timeout) sock.connect(self.host[len('unix://'):]) else: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if self._is_ipv4_host(): + sock = socket.socket(socket.AF_INET, + socket.SOCK_STREAM) + else: + sock = socket.socket(socket.AF_INET6, + socket.SOCK_STREAM) sock.settimeout(self.timeout) # This might be controversial and may need to be removed sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)