Created
January 16, 2016 21:09
-
-
Save jacqueswww/1d048315d7a3a0a6b1b9 to your computer and use it in GitHub Desktop.
Detect an ip address
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
import sys | |
def ipv6(s): | |
group_split = s.split(':') | |
if len(group_split) == 8: | |
for sub in group_split: | |
try: | |
ip_val = int(sub, 16) | |
if 0 <= ip_val <= 65535: | |
continue | |
else: | |
return None | |
except: | |
return None | |
return "IPv6" | |
return None | |
def ipv4(s): | |
group_split = s.split('.') | |
if len(group_split) == 4: | |
is_valid = False | |
for sub in group_split: | |
try: | |
ip_val = int(sub) | |
if 0 <= ip_val <= 256: | |
continue | |
else: | |
return None | |
except: | |
return None | |
return "IPv4" | |
return None | |
def print_type(s): | |
res = ipv4(s) | |
if res is not None: | |
return res | |
res = ipv6(s) | |
if res is not None: | |
return res | |
return "Neither" | |
i = 0 | |
for line in sys.stdin: | |
if i > 0: | |
print(print_type(line)) | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment