Ejemplo n.º 1
0
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: C{True} if IPv4 address is valid, C{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:
        validity = False

    return validity
Ejemplo n.º 2
0
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:
        validity = False

    return validity
Ejemplo n.º 3
0
Archivo: ipv4.py Proyecto: embr/nonce
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:
        validity = False

    return validity
Ejemplo n.º 4
0
def valid_str(addr):
    """
    @param addr: An IPv4 address in presentation (string) format.

    @return: C{True} if IPv4 address is valid, C{False} otherwise.
    """
    if addr == "":
        raise AddrFormatError("Empty strings are not supported!")

    try:
        _inet_aton(addr)
    except:
        return False
    return True
Ejemplo n.º 5
0
def valid_str(addr):
    """
    @param addr: An IPv4 address in presentation (string) format.

    @return: C{True} if IPv4 address is valid, C{False} otherwise.
    """
    if addr == '':
        raise AddrFormatError('Empty strings are not supported!')

    try:
        _inet_aton(addr)
    except:
        return False
    return True
Ejemplo n.º 6
0
def str_to_int(addr):
    """
    @param addr: An IPv4 dotted decimal address in string form.

    @return: The equivalent unsigned integer for a given IPv4 address.
    """
    if addr == "":
        raise AddrFormatError("Empty strings are not supported!")
    try:
        return _struct.unpack(">I", _inet_aton(addr))[0]
    except:
        #   Windows platform workaround.
        if hasattr(addr, "lower") and _platform.system() == "Windows":
            if addr.lower() == "0xffffffff":
                return 0xFFFFFFFF

        raise AddrFormatError("%r is not a valid IPv4 address string!" % addr)
Ejemplo n.º 7
0
def str_to_int(addr):
    """
    @param addr: An IPv4 dotted decimal address in string form.

    @return: The equivalent unsigned integer for a given IPv4 address.
    """
    if addr == '':
        raise AddrFormatError('Empty strings are not supported!')
    try:
        return _struct.unpack('>I', _inet_aton(addr))[0]
    except:
        #   Windows platform workaround.
        if hasattr(addr, 'lower') and _platform.system() == 'Windows':
            if addr.lower() == '0xffffffff':
                return 0xffffffff

        raise AddrFormatError('%r is not a valid IPv4 address string!' \
            % addr)
Ejemplo n.º 8
0
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:
        raise AddrFormatError('%r is not a valid IPv4 address string!' % addr)
Ejemplo n.º 9
0
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:
        raise AddrFormatError('%r is not a valid IPv4 address string!' % addr)