-
Notifications
You must be signed in to change notification settings - Fork 4
/
ValidateIpAddress.py
44 lines (37 loc) · 1.29 KB
/
ValidateIpAddress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def validIPAddress(queryIP: str) -> str:
if queryIP.find(".") != -1:
# IPv4
last = -1
for i in range(4):
cur = (len(queryIP) if i == 3 else queryIP.find(".", last + 1))
if cur == -1:
return "Neither"
if not 1 <= cur - last - 1 <= 3:
return "Neither"
addr = 0
for j in range(last + 1, cur):
if not queryIP[j].isdigit():
return "Neither"
addr = addr * 10 + int(queryIP[j])
if addr > 255:
return "Neither"
if addr > 0 and queryIP[last + 1] == "0":
return "Neither"
if addr == 0 and cur - last - 1 > 1:
return "Neither"
last = cur
return "IPv4"
else:
# IPv6
last = -1
for i in range(8):
cur = (len(queryIP) if i == 7 else queryIP.find(":", last + 1))
if cur == -1:
return "Neither"
if not 1 <= cur - last - 1 <= 4:
return "Neither"
for j in range(last + 1, cur):
if not queryIP[j].isdigit() and not ("a" <= queryIP[j].lower() <= "f"):
return "Neither"
last = cur
return "IPv6"