def isString(cls, address): """Test if address is a valid string for address family""" try: win.inet_pton(cls.af, address) return True except socket.error: return False
def _as_v6(self, value): if platform.system() == "Windows": import win_inet_pton return Address(True, win_inet_pton.inet_pton(socket.AF_INET6, value)) else: return Address(True, socket.inet_pton(socket.AF_INET6, value))
def fromString(cls, address): """Create address from string Raises socket.error on failure""" shiftword = lambda x, y: (2 ** 32) * x + y structdesc = ">{:d}L".format(cls.bits / 32) return cls(reduce(shiftword, struct.unpack(structdesc, win.inet_pton(cls.af, address))))
def ip_to_integer(ip_address): # try parsing the IP address first as IPv4, then as IPv6 for version in (socket.AF_INET, socket.AF_INET6): try: ip_hex = win_inet_pton.inet_pton(version, ip_address) if platform == 'Windows' else socket.inet_pton(version, ip_address) ip_integer = int(binascii.hexlify(ip_hex), 16) return ip_integer, 4 if version == socket.AF_INET else 6 except: pass raise ValueError("invalid IP address")
def ip_to_integer(ip_address): # try parsing the IP address first as IPv4, then as IPv6 for version in (socket.AF_INET, socket.AF_INET6): try: ip_hex = win_inet_pton.inet_pton(version, ip_address) if platform == 'Windows' else socket.inet_pton(version, ip_address) ip_integer = int(binascii.hexlify(ip_hex), 16) return ip_integer, 4 if version == socket.AF_INET else 6 except: pass raise ValueError("invalid IP address")
def private_ip(self, ip): f = unpack('!I', inet_pton(AF_INET, ip))[0] private = ( [2130706432, 4278190080 ], # 127.0.0.0, 255.0.0.0 http://tools.ietf.org/html/rfc3330 [3232235520, 4294901760 ], # 192.168.0.0, 255.255.0.0 http://tools.ietf.org/html/rfc1918 [2886729728, 4293918720 ], # 172.16.0.0, 255.240.0.0 http://tools.ietf.org/html/rfc1918 [167772160, 4278190080 ], # 10.0.0.0, 255.0.0.0 http://tools.ietf.org/html/rfc1918 ) for net in private: if (f & net[1]) == net[0]: return True return False