]> git-server-git.apps.pok.os.sepia.ceph.com Git - radosgw-agent.git/commitdiff
add a network utility to check for ipv6
authorAlfredo Deza <adeza@redhat.com>
Fri, 19 Jun 2015 16:39:16 +0000 (12:39 -0400)
committerAlfredo Deza <adeza@redhat.com>
Fri, 19 Jun 2015 16:39:16 +0000 (12:39 -0400)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
radosgw_agent/util/network.py [new file with mode: 0644]

diff --git a/radosgw_agent/util/network.py b/radosgw_agent/util/network.py
new file mode 100644 (file)
index 0000000..baa4e13
--- /dev/null
@@ -0,0 +1,20 @@
+import socket
+
+
+def is_ipv6(address):
+    """
+    Check if an address is an IPV6 one, but trim commonly used brackets as the
+    ``socket`` module complains about them.
+    """
+    if not isinstance(address, str):
+        return False
+
+    if address.startswith('['):  # assume we need to split on possible port
+        address = address.split(']:')[0]
+    # strip leading/trailing brackets so inet_pton understands the address
+    address = address.strip('[]')
+    try:
+        socket.inet_pton(socket.AF_INET6, address)
+    except socket.error:  # not a valid address
+        return False
+    return True