Skip to content

Instantly share code, notes, and snippets.

@alexgottscha
Created January 13, 2015 00:27
Show Gist options
  • Save alexgottscha/fdf7e2d985c8287db96d to your computer and use it in GitHub Desktop.
Save alexgottscha/fdf7e2d985c8287db96d to your computer and use it in GitHub Desktop.
is_ipaddr.py
def is_ip4addr(some_string):
'''True if some_string is an IPv4 address'''
try:
octets = [int(o) for o in some_string.split('.')]
except ValueError: # because some element cannot be cast to integer
return False
# ip addresses have between one and four octets
if len(octets) > 4:
return False
# True if all octets fall into 0-255 range
return all([ o <= 255 and o >=0 for o in octets ])
@alexgottscha
Copy link
Author

But seriously, just use netaddr

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