improved ipv6 address checked, thanks Jonathan

This commit is contained in:
mdipierro
2013-05-06 07:33:54 +10:00
committed by Tim Richardson
parent 98ad12a06c
commit cabef57851
4 changed files with 11 additions and 13 deletions
+5 -7
View File
@@ -298,18 +298,16 @@ def is_valid_ip_address(address):
def is_loopback_ip_address(ip=None, addrinfo=None):
"""
Determines whether the address appears to be a loopback address.
This assumes that the IP is valid. The IPv6 check is limited to '::1'.
This assumes that the IP is valid.
"""
if addrinfo: # see socket.getaddrinfo() for layout of addrinfo tuple
if addrinfo[0] == socket.AF_INET or addrinfo[0] == socket.AF_INET6:
ip = addrinfo[4]
if not ip:
if not isinstance(ip, basestring):
return False
if isinstance(ip, basestring) and ip.count('.') == 3: # IPv4
return ip.startswith('127') or ip.startswith('::ffff:127')
return ip == '::1' # IPv6
if ip.count('.') == 3: # IPv4
return ip.startswith('127') or ip.startswith('::127') or ip.startswith(' 0:0:0:0:0:0:127')
return ip == '::1' or ip == '0:0:0:0:0:0:0:1' # IPv6 loopback
def getipaddrinfo(host):