def func388(s):
ipv4 = r"^((25|2\d|?\d\d?)\.){3}(25|2\d|?\d\d?)$"
ipv6 = r"^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$"
if re.fullmatch(ipv4, s):
return "IPv4"
elif re.fullmatch(ipv6, s):
return "IPv6"
else:
return 'Neither' xiangjianshinan 发表于 2020-5-1 23:38
粗制滥造一代码!
输入 "2001:0db8:85a3:0:0:8A2E:0370:7334:" 结果有误 Twilight6 发表于 2020-5-1 23:29
凑合吧 最后附上自己的测试
输入 '1.1.1.1.' 出错 ouyunfu 发表于 2020-5-2 04:25
输入 "1e1.4.5.6" 出错 import re
def check_ip(a=None):
a = "2001:0db8:85a3:0:0:8A2E:0370:7334"
ipv4_flag = False
if ':' not in a:
没有鱼币的苦逼-.-
for k in a.split('.'):
if str(int(k)) != k:
break
if 255 < int(k) or int(k) < 0:
break
else:
ipv4_flag = True
if ipv4_flag:
print("IPV4")
else:
if re.match("([\dabcdef]{1,4}:){7}[\dabcdef]{1,4}$", a, re.I):
print("IPV6")
else:
print("Neither") import re
def check_ip(a=None):
a = "2001:0db8:85a3:0:0:8A2E:0370:7334"
ipv4_flag = False
try:
if ':' not in a:
for k in a.split('.'):
if str(int(k)) != k:
break
if 255 < int(k) or int(k) < 0:
break
else:
ipv4_flag = True
except:
pass
if ipv4_flag:
print("IPV4")
else:
if re.match("({1,4}:){7}{1,4}$", a, re.I):
print("IPV6")
else:
print("Neither") def fun388(x):
hex_temp = ':0123456789abcdefABCDEF'
dec_temp = '.0123456789'
if '.' in x and ':' not in x:
if len(x.split('.')) > 4:
return 'Neither'
for i in x:
if i not in dec_temp:
return 'Neither'
for i in x.split('.'):
if i == '0' or int(i) > 255:
return 'Neither'
else:
return 'IPv4'
if ':' in x and '.' not in x:
if len(x.split(':')) > 16 or len(x.split(':')) < 8:
return 'Neither'
for i in x:
if i not in hex_temp:
return 'Neither'
for i in x.split(':'):
if len(i) > 4:
return 'Neither'
else:
return 'IPv6'
return 'Neither' def fun388(address):
if '.' in address and address.count('.') == 3:
splited = address.split('.')
for each in splited:
if each == '0' or not each.isdecimal() or int(each) < 0or int(each) > 255:
break
else:
return 'IPv4'
elif ':' in address and address.count(':') == 7:
splited = address.split(':')
for each in splited:
if not each or len(each) > 4:
break
else:
return 'IPv6'
return 'Neither' {:10_316:}
页:
1
[2]