def check_ip(s):
if len(s) != 4:
return False
for c in s:
if not c.isdigit(): # 不是数字,或者为空
return False
else:
c = int(c)
if c>255 or c<0:# 不在0~255范围内
return False
return True
def check_mask(s):
if check_ip(s):
last = 0
for c in s:
c = int(c)
if c!=255 and c>last: # 四个数中出现0在1前面的情况
return False
last = c
flag = -1
while c: # 每个数中出现0在1前面的情况
if c%2==1:
flag == 1
else:
if flag == 1:
return False
flag = 0
c = c>>1
else:
return False
return True
检查ip和子网掩码是否合法(python)
最新推荐文章于 2024-09-11 14:17:12 发布