diff options
| author | Bobby <[email protected]> | 2022-09-10 20:36:32 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-09-10 20:36:32 -0400 |
| commit | 55a13a4ab4004ca38177e1c8735df2341ad935b7 (patch) | |
| tree | 31312271b27e4aee9fb1a1636ca12cccac2c8b15 /src | |
| parent | 5085e5d09086766e2b3a55f6d9e2f797015f25d0 (diff) | |
| download | edify-55a13a4ab4004ca38177e1c8735df2341ad935b7.tar.xz edify-55a13a4ab4004ca38177e1c8735df2341ad935b7.zip | |
Added ipv4 and ipv6 validations
Diffstat (limited to 'src')
| -rw-r--r-- | src/edify/library/__init__.py | 2 | ||||
| -rw-r--r-- | src/edify/library/ip.py | 34 |
2 files changed, 36 insertions, 0 deletions
diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py index e77c7f6..6bf12a5 100644 --- a/src/edify/library/__init__.py +++ b/src/edify/library/__init__.py @@ -1,6 +1,8 @@ # flake8: noqa # Import everything from the library. +from .ip import ipv4 +from .ip import ipv6 from .mail import email from .mail import email_rfc_5322 from .phone import phone_number diff --git a/src/edify/library/ip.py b/src/edify/library/ip.py new file mode 100644 index 0000000..d04b69b --- /dev/null +++ b/src/edify/library/ip.py @@ -0,0 +1,34 @@ +import re + +ipv4_pattern = "^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" # noqa +ipv6_pattern = "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$" # noqa + + +def ipv4(ip: str) -> bool: + """Checks if a string is a valid IPv4 address. + + Args: + ip (str): The string to check. + Returns: + bool: True if the string is a valid IPv4 address, False otherwise. + """ + + if re.match(ipv4_pattern, ip): + return True + else: + return False + + +def ipv6(ip: str) -> bool: + """Checks if a string is a valid IPv6 address. + + Args: + ip (str): The string to check. + Returns: + bool: True if the string is a valid IPv6 address, False otherwise. + """ + + if re.match(ipv6_pattern, ip): + return True + else: + return False |
