Skip to content

ip_address

validators.ip_address.ipv4(value, /, *, cidr=True, strict=False, host_bit=True)

Returns whether a given value is a valid IPv4 address.

From Python version 3.9.5 leading zeros are no longer tolerated and are treated as an error. The initial version of ipv4 validator was inspired from WTForms IPAddress validator.

Examples:

>>> ipv4('123.0.0.7')
# Output: True
>>> ipv4('1.1.1.1/8')
# Output: True
>>> ipv4('900.80.70.11')
# Output: ValidationError(func=ipv4, args={'value': '900.80.70.11'})

Parameters:

Name Type Description Default
value str

IP address string to validate.

required
cidr bool

IP address string may contain CIDR notation

True
strict bool

IP address string is strictly in CIDR notation

False
host_bit bool

If False and host bits (along with network bits) are set in the supplied address, this function raises a validation error. ref IPv4Network.

True

Returns:

Type Description
Literal[True]

If value is a valid IPv4 address.

ValidationError

If value is an invalid IPv4 address.

Note
  • In version 0.14.0:
    • Add supports for CIDR notation

New in version 0.2.0

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/ip_address.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@validator
def ipv4(value: str, /, *, cidr: bool = True, strict: bool = False, host_bit: bool = True):
    """Returns whether a given value is a valid IPv4 address.

    From Python version 3.9.5 leading zeros are no longer tolerated
    and are treated as an error. The initial version of ipv4 validator
    was inspired from [WTForms IPAddress validator][1].

    [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py

    Examples:
        >>> ipv4('123.0.0.7')
        # Output: True
        >>> ipv4('1.1.1.1/8')
        # Output: True
        >>> ipv4('900.80.70.11')
        # Output: ValidationError(func=ipv4, args={'value': '900.80.70.11'})

    Args:
        value:
            IP address string to validate.
        cidr:
            IP address string may contain CIDR notation
        strict:
            IP address string is strictly in CIDR notation
        host_bit:
            If `False` and host bits (along with network bits) _are_ set in the supplied
            address, this function raises a validation error. ref [IPv4Network][2].
            [2]: https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Network

    Returns:
        (Literal[True]):
            If `value` is a valid IPv4 address.
        (ValidationError):
            If `value` is an invalid IPv4 address.

    Note:
        - *In version 0.14.0*:
            - Add supports for CIDR notation

    > *New in version 0.2.0*
    """
    if not value:
        return False
    try:
        if cidr:
            if strict and value.count("/") != 1:
                raise ValueError("IPv4 address was expected in CIDR notation")
            return IPv4Network(value, strict=not host_bit)
        return IPv4Address(value)
    except (ValueError, AddressValueError, NetmaskValueError):
        return False

validators.ip_address.ipv6(value, /, *, cidr=True, strict=False, host_bit=True)

Returns if a given value is a valid IPv6 address.

Including IPv4-mapped IPv6 addresses. The initial version of ipv6 validator was inspired from WTForms IPAddress validator.

Examples:

>>> ipv6('::ffff:192.0.2.128')
# Output: True
>>> ipv6('::1/128')
# Output: True
>>> ipv6('abc.0.0.1')
# Output: ValidationError(func=ipv6, args={'value': 'abc.0.0.1'})

Parameters:

Name Type Description Default
value str

IP address string to validate.

required
cidr bool

IP address string may contain CIDR annotation

True
strict bool

IP address string is strictly in CIDR notation

False
host_bit bool

If False and host bits (along with network bits) are set in the supplied address, this function raises a validation error. ref IPv6Network.

True

Returns:

Type Description
Literal[True]

If value is a valid IPv6 address.

ValidationError

If value is an invalid IPv6 address.

Note
  • In version 0.14.0:
    • Add supports for CIDR notation

New in version 0.2.0

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/ip_address.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
@validator
def ipv6(value: str, /, *, cidr: bool = True, strict: bool = False, host_bit: bool = True):
    """Returns if a given value is a valid IPv6 address.

    Including IPv4-mapped IPv6 addresses. The initial version of ipv6 validator
    was inspired from [WTForms IPAddress validator][1].

    [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py

    Examples:
        >>> ipv6('::ffff:192.0.2.128')
        # Output: True
        >>> ipv6('::1/128')
        # Output: True
        >>> ipv6('abc.0.0.1')
        # Output: ValidationError(func=ipv6, args={'value': 'abc.0.0.1'})

    Args:
        value:
            IP address string to validate.
        cidr:
            IP address string may contain CIDR annotation
        strict:
            IP address string is strictly in CIDR notation
        host_bit:
            If `False` and host bits (along with network bits) _are_ set in the supplied
            address, this function raises a validation error. ref [IPv6Network][2].
            [2]: https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv6Network

    Returns:
        (Literal[True]):
            If `value` is a valid IPv6 address.
        (ValidationError):
            If `value` is an invalid IPv6 address.

    Note:
        - *In version 0.14.0*:
            - Add supports for CIDR notation

    > *New in version 0.2.0*
    """
    if not value:
        return False
    try:
        if cidr:
            if strict and value.count("/") != 1:
                raise ValueError("IPv6 address was expected in CIDR notation")
            return IPv6Network(value, strict=not host_bit)
        return IPv6Address(value)
    except (ValueError, AddressValueError, NetmaskValueError):
        return False