From: Alfredo Deza Date: Fri, 19 Jun 2015 16:39:16 +0000 (-0400) Subject: add a network utility to check for ipv6 X-Git-Tag: v1.2.3~3^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6064f3897699903b85a469a9ee77e44116f7df4c;p=radosgw-agent.git add a network utility to check for ipv6 Signed-off-by: Alfredo Deza --- diff --git a/radosgw_agent/util/network.py b/radosgw_agent/util/network.py new file mode 100644 index 0000000..baa4e13 --- /dev/null +++ b/radosgw_agent/util/network.py @@ -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