Skip to content

Instantly share code, notes, and snippets.

@Jonarzz
Last active November 19, 2015 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jonarzz/f9cc3e855ee673317bf3 to your computer and use it in GitHub Desktop.
Save Jonarzz/f9cc3e855ee673317bf3 to your computer and use it in GitHub Desktop.
The script takes N as number of lines to read; then takes N lines and checks if the line is IPv4, IPv6 or neither.
import re
n = int(input())
for _ in range(n):
output = 'Neither'
line = str(input())
match = re.match(r'^(\d{0,3})\.(\d{0,3})\.(\d{0,3})\.(\d{0,3})$', line)
if match is not None:
groups = match.groups()
output = 'IPv4'
for group in groups:
if int(group) > 255:
output = 'Neither'
break
else:
match = re.search(r'^([0-9a-f]{0,4}:){7}[0-9a-f]{0,4}$', line)
if match is not None:
output = 'IPv6'
else:
output = 'Neither'
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment