def valid_str(addr, flags=0): """ :param addr: An IPv4 address in presentation (string) format. :param flags: decides which rules are applied to the interpretation of the addr value. Supported constants are INET_PTON and ZEROFILL. See the netaddr.core docs for details. :return: ``True`` if IPv4 address is valid, ``False`` otherwise. """ if addr == '': raise AddrFormatError('Empty strings are not supported!') validity = True if flags & ZEROFILL: addr = '.'.join(['%d' % int(i) for i in addr.split('.')]) try: if flags & INET_PTON: _inet_pton(AF_INET, addr) else: _inet_aton(addr) except Exception: validity = False return validity
def valid_str(addr, flags=0): """ :param addr: An IPv4 address in presentation (string) format. :param flags: decides which rules are applied to the interpretation of the addr value. Supported constants are INET_PTON and ZEROFILL. See the netaddr.core docs for details. :return: ``True`` if IPv4 address is valid, ``False`` otherwise. """ if addr == "": raise AddrFormatError("Empty strings are not supported!") validity = True if flags & ZEROFILL: addr = ".".join(["%d" % int(i) for i in addr.split(".")]) try: if flags & INET_PTON: _inet_pton(AF_INET, addr) else: _inet_aton(addr) except Exception: validity = False return validity
def valid_str(self, addr): """ @param addr: An IP address in presentation (string) format. @return: C{True} if network address in string form is valid for this address type, C{False} otherwise. """ if addr == '': raise AddrFormatError('Empty strings are not supported!') try: _inet_aton(addr) except: return False return True
def inet_aton(ip_string): # On some 64 bit platforms, with some versions of Python, socket.inet_aton # returns the a full 64 bit register, rather than the 32 bit value. # The result of this is that the returned bit string is right-padded with # 32 bits (4 chars) of zeroes. See: # http://bugs.python.org/issue767150 # http://bugs.python.org/issue1008086 # This wrapper ensures the result is always truncated to the first 32 bits. return _inet_aton(ip_string)[:4]
def str_to_int(self, addr): """ @param addr: An IPv4 dotted decimal address in string form. @return: An unsigned integer that is equivalent to value represented by the IPv4 dotted decimal address string. """ if addr == '': raise AddrFormatError('Empty strings are not supported!') try: return _struct.unpack('>I', _inet_aton(addr))[0] except: raise AddrFormatError('%r is not a valid IPv4 address string!' \ % addr)
def str_to_int(addr, flags=0): """ :param addr: An IPv4 dotted decimal address in string form. :param flags: decides which rules are applied to the interpretation of the addr value. Supported constants are INET_PTON and ZEROFILL. See the netaddr.core docs for details. :return: The equivalent unsigned integer for a given IPv4 address. """ if flags & ZEROFILL: addr = ".".join(["%d" % int(i) for i in addr.split(".")]) try: if flags & INET_PTON: return _struct.unpack(">I", _inet_pton(AF_INET, addr))[0] else: return _struct.unpack(">I", _inet_aton(addr))[0] except Exception: raise AddrFormatError("%r is not a valid IPv4 address string!" % addr)
def str_to_int(addr, flags=0): """ :param addr: An IPv4 dotted decimal address in string form. :param flags: decides which rules are applied to the interpretation of the addr value. Supported constants are INET_PTON and ZEROFILL. See the netaddr.core docs for details. :return: The equivalent unsigned integer for a given IPv4 address. """ if flags & ZEROFILL: addr = '.'.join(['%d' % int(i) for i in addr.split('.')]) try: if flags & INET_PTON: return _struct.unpack('>I', _inet_pton(AF_INET, addr))[0] else: return _struct.unpack('>I', _inet_aton(addr))[0] except Exception: raise AddrFormatError('%r is not a valid IPv4 address string!' % addr)