Skip to content

fix ipv4/ipv6 fallback #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions pyzabbix/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,21 +385,29 @@ def _chunk_send(self, metrics):
packet = self._create_packet(request)

for host_addr in self.zabbix_uri:
logger.debug('Sending data to %s', host_addr)

try:
# IPv4
connection_ = socket.socket(socket.AF_INET)
except socket.error:
# IPv6
logger.debug("Sending data to %s", host_addr)

host, port = host_addr
conn_ = None
for res in socket.getaddrinfo(
host, port, socket.AF_UNSPEC, socket.SOCK_STREAM
):
af, socktype, proto, canonname, sa = res
try:
connection_ = socket.socket(socket.AF_INET6)
except socket.error:
raise Exception("Error creating socket for {host_addr}".format(host_addr=host_addr))
conn_ = socket.socket(af, socktype, proto)
except OSError as msg:
conn_ = None
continue
break

if conn_ is None:
raise Exception(
"Error creating socket for {host_addr}".format(host_addr=host_addr)
)
if self.socket_wrapper:
connection = self.socket_wrapper(connection_)
connection = self.socket_wrapper(conn_)
else:
connection = connection_
connection = conn_

connection.settimeout(self.timeout)

Expand Down