Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.