sivy / pystatsd

Python implementation of the Statsd client/server
http://pypi.python.org/pypi/pystatsd/
BSD 2-Clause "Simplified" License
358 stars 87 forks source link

ipv6 is not supported #96

Open clayg opened 8 years ago

clayg commented 8 years ago

mostly bind does the right thing if you set AF_INET6 as the family type - maybe try that as a fallback?

diff --git a/pystatsd/server.py b/pystatsd/server.py
index 40118c8..0bb9f9a 100644
--- a/pystatsd/server.py
+++ b/pystatsd/server.py
@@ -300,8 +300,23 @@ class Server(object):
     def serve(self, hostname='', port=8125):
         assert type(port) is int, 'port is not an integer: %s' % (port)
         addr = (hostname, port)
-        self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
-        self._sock.bind(addr)
+        try:
+            self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+            self._sock.bind(addr)
+        except socket.gaierror as e:
+            if e.errno not in (
+                    # Address family for hostname not supported
+                    socket.EAI_ADDRFAMILY,
+                    # Name or service not known
+                    socket.EAI_NONAME):
+                raise
+            # IPv6 address calls for (host, port, flowinfo, scopeid)
+            # although, flowinfo and scopeid would default to 0 in
+            # socketmodule.c we're explict here
+            flowinfo = scopeid = 0
+            addr = (hostname, port, flowinfo, scopeid)
+            self._sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
+            self._sock.bind(addr)

         import signal