Skip to content

util

util

IPv4 / IPv6 address and CIDR validation helpers.

is_ipv4(value)

Return True if value is an IPv4 address or CIDR notation.

Source code in src/terok_shield/common/util.py
def is_ipv4(value: str) -> bool:
    """Return True if *value* is an IPv4 address or CIDR notation."""
    try:
        if "/" in value:
            ipaddress.IPv4Network(value, strict=False)
        else:
            ipaddress.IPv4Address(value)
        return True
    except ValueError:
        return False

is_ipv6(value)

Return True if value is an IPv6 address or CIDR notation.

Source code in src/terok_shield/common/util.py
def is_ipv6(value: str) -> bool:
    """Return True if *value* is an IPv6 address or CIDR notation."""
    try:
        if "/" in value:
            ipaddress.IPv6Network(value, strict=False)
        else:
            ipaddress.IPv6Address(value)
        return True
    except ValueError:
        return False

is_ip(value)

Return True if value is an IPv4 or IPv6 address or CIDR.

Source code in src/terok_shield/common/util.py
def is_ip(value: str) -> bool:
    """Return True if *value* is an IPv4 or IPv6 address or CIDR."""
    return is_ipv4(value) or is_ipv6(value)