Skip to content

Instantly share code, notes, and snippets.

@stevensdotb
Last active July 13, 2018 05:57
Show Gist options
  • Save stevensdotb/b9924e437a1116fed43bf84aad2143a2 to your computer and use it in GitHub Desktop.
Save stevensdotb/b9924e437a1116fed43bf84aad2143a2 to your computer and use it in GitHub Desktop.
IP Address validator
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