OXIESEC PANEL
- Current Dir:
/
/
opt
/
alt
/
python311
/
lib
/
python3.11
/
site-packages
/
validators
Server IP: 2a02:4780:11:1594:0:ef5:22d7:a
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
01/09/2025 02:18:04 AM
rwxr-xr-x
📄
__init__.py
1.86 KB
05/14/2024 03:18:23 PM
rw-r--r--
📁
__pycache__
-
05/14/2024 03:18:23 PM
rwxr-xr-x
📄
_extremes.py
1.01 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
_tld.txt
9.4 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
between.py
2.39 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
btc_address.py
1.62 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
card.py
5.63 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
country.py
14.57 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
cron.py
2.23 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
domain.py
2.4 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
email.py
2.72 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
encoding.py
1.34 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
finance.py
3.22 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
hashes.py
3.21 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
hostname.py
4.05 KB
05/14/2024 03:18:23 PM
rw-r--r--
📁
i18n
-
05/14/2024 03:18:23 PM
rwxr-xr-x
📄
iban.py
1.05 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
ip_address.py
4.34 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
length.py
1.45 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
mac_address.py
865 bytes
05/14/2024 03:18:23 PM
rw-r--r--
📄
py.typed
0 bytes
05/14/2024 03:18:23 PM
rw-r--r--
📄
slug.py
750 bytes
05/14/2024 03:18:23 PM
rw-r--r--
📄
uri.py
1.78 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
url.py
7.23 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
utils.py
3.1 KB
05/14/2024 03:18:23 PM
rw-r--r--
📄
uuid.py
1.04 KB
05/14/2024 03:18:23 PM
rw-r--r--
Editing: ip_address.py
Close
"""IP Address.""" # standard from ipaddress import ( AddressValueError, IPv4Address, IPv4Network, IPv6Address, IPv6Network, NetmaskValueError, ) import re from typing import Optional # local from .utils import validator def _check_private_ip(value: str, is_private: Optional[bool]): if is_private is None: return True if is_private and ( any( value.startswith(l_bit) for l_bit in { "10.", # private "192.168.", # private "169.254.", # link-local "127.", # localhost "0.0.0.0", # loopback #nosec } ) or re.match(r"^172\.(?:1[6-9]|2\d|3[0-1])\.", value) # private or re.match(r"^(?:22[4-9]|23[0-9]|24[0-9]|25[0-5])\.", value) # broadcast ): return True return False @validator def ipv4( value: str, /, *, cidr: bool = True, strict: bool = False, private: Optional[bool] = None, 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. private: IP address is public if `False`, private/local/loopback/broadcast if `True`. 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. """ 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) and _check_private_ip(value, private) return IPv4Address(value) and _check_private_ip(value, private) except (ValueError, AddressValueError, NetmaskValueError): return False @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. """ 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