Last active
July 13, 2018 05:57
-
-
Save stevensdotb/b9924e437a1116fed43bf84aad2143a2 to your computer and use it in GitHub Desktop.
IP Address validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def validate(ip): | |
is_valid = True | |
split_ip = str(ip).split('.') | |
# 4 numbers separated by dots | |
if len(split_ip) == 4: | |
for number in split_ip: | |
# Each number has to be an integer between 0 and 255 | |
if not int(number) and (not int(number) >= 0 or not int(number) <= 255): | |
is_valid = False | |
break | |
print("IP Address: %s" % str(self.value) if is_valid else "[!] Invalid IP Address!") | |
def validate_with_regex(ip): | |
pattern = re.compile(r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$') | |
print("IP Address: %s" % str(ip) if pattern.match(str(ip)) else "[!] Invalid IP Address!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment