Created
January 13, 2015 00:27
-
-
Save alexgottscha/fdf7e2d985c8287db96d to your computer and use it in GitHub Desktop.
is_ipaddr.py
This file contains 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
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 ]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But seriously, just use
netaddr