Skip to content

Instantly share code, notes, and snippets.

@benhagen
Last active March 19, 2020 19:23
Show Gist options
  • Save benhagen/5296795 to your computer and use it in GitHub Desktop.
Save benhagen/5296795 to your computer and use it in GitHub Desktop.
Python function to determine if a string is like an IPv4 address. Its lame but works; to be improved at a later date.
def is_ipv4(ip):
match = re.match("^(\d{0,3})\.(\d{0,3})\.(\d{0,3})\.(\d{0,3})$", ip)
if not match:
return False
quad = []
for number in match.groups():
quad.append(int(number))
if quad[0] < 1:
return False
for number in quad:
if number > 255 or number < 0:
return False
return True
@Pr1meSuspec7
Copy link

Hi Ben,
this is my version of the function: https://gist.github.com/Pr1meSuspec7/d6154c4f9b01d2ea80cd5fd51ddfb8eb
I hope it can help you

Bye

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment