7. 判断ip地址是否合法 python
import sys a = list(map(str,input().split("."))) def panduan255(a): for i in range(len(a)): if a[i] == ""or not a[i].isdigit(): return 0 if len(a[i]) > 1 and a[i][0] == '0': return 0 if i ==0: if not 1 <= int(a[i]) <= 255 : return 0 else: if not 0 <= int(a[i]) <= 255 : return 0 return 1 iptype = panduan255(a) if len(a)!=4: iptype =0 if a[0]=='0' and a[1]=='0' and a[2]=='0' and a[3]=='0' : iptype =1 if iptype ==0: print("invalid") else: a = list(map(int, a)) if 1<=a[0]<=126 and not (a[0]==126 and a[3]!= 0): print("A_address") elif 128 <=a[0] <=191: print("B_address") elif 192 <=a[0] <=223: print("C_address") else: print("other")
某网络系统需要对输入的IP地址进行合法性判断。IP地址由四个十进制数字组成,每个数字的取值范围是0到255(包含0和255)。
IP地址的格式为X.X.X.X,其中X表示一个十进制数字。系统要求判断输入的IP地址是否合法,即满足以下条件:
1.IP地址由四个数字组成,用点号分隔。
2.每个数字的取值范围是0到255。
3.数字之间没有多余的前导零,例如01是非法的。
4.IP地址不能以点号开始或结束,例如.192.168.0.1和192.168.0.1.是非法的。
不合法的情况下输出"invalid",合法的情况下,你还需要判断是哪一类地址:
A类地址:地址范围从1.0.0.0到126.0.0.0
B类地址:地址范围从128.0.0.0到191.255.255.255
C类地址:范围从192.0.0.0到223.255.255.255
其它地址:合法输入,但是不是A、B、C类
请通过代码实现上述功能