当前位置 博文首页 > 一个混错圈儿的小测试:python:识别有效的IP地址和掩码并进行分

    一个混错圈儿的小测试:python:识别有效的IP地址和掩码并进行分

    作者:[db:作者] 时间:2021-08-20 15:41

    题目描述

    请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。

    所有的IP地址划分为?A,B,C,D,E五类

    A类地址1.0.0.0~126.255.255.255;

    B类地址128.0.0.0~191.255.255.255;

    C类地址192.0.0.0~223.255.255.255;

    D类地址224.0.0.0~239.255.255.255;

    E类地址240.0.0.0~255.255.255.255

    ?

    私网IP范围是:

    10.0.0.0~10.255.255.255

    172.16.0.0~172.31.255.255

    192.168.0.0~192.168.255.255

    ?

    子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码)

    注意二进制下全是1或者全是0均为非法

    ?

    注意:

    1. 类似于【0.*.*.*】和【127.*.*.*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时可以忽略

    2. 私有IP地址和A,B,C,D,E类地址是不冲突的

    ?

    输入描述:

    多行字符串。每行一个IP地址和掩码,用~隔开。

    ?

    输出描述:

    统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。

    示例1

    输入

    10.70.44.68~255.254.255.0
    1.0.0.1~255.0.0.0
    192.168.0.2~255.255.255.0
    19..0.~255.255.255.0
    

    ?

    输出?

    1 0 1 0 0 2 1

    ?

    实现

    def ip4_check(ip_s):
    ? ? ip_4 = ip_s.strip().split(".")
    ? ? if len(ip_4) == 4: ?# 判断IP是否为4位数
    ? ? ? ? for i in range(4):
    ? ? ? ? ? ? # 判断IP是否为0-255的整数
    ? ? ? ? ? ? if ip_4[i].isdigit() and ip_4[i] == str(int(ip_4[i])) and 0 <= int(ip_4[i]) <= 255:
    ? ? ? ? ? ? ? ? continue
    ? ? ? ? ? ? else:
    ? ? ? ? ? ? ? ? return False
    ? ? ? ? return True
    ? ? else:
    ? ? ? ? return False


    def subnet_mask(s):
    ? ? if s == '255.255.255.255' or s == '0.0.0.0':
    ? ? ? ? return False
    ? ? sub_mask = s.split('.')
    ? ? if len(sub_mask) == 4:
    ? ? ? ? l = []
    ? ? ? ? for i in sub_mask:

    ? ? ? ? ? ??if int(i) > 255 or int(i) < 0:
    ? ? ? ? ? ? ? ? return False
    ? ? ? ? ? ? bin_ = bin(int(i)).split('b')[1]
    ? ? ? ? ? ? if len(bin_) < 8:
    ? ? ? ? ? ? ? ? bin_ = '0'*(8 - len(bin_)) + bin_
    ? ? ? ? ? ? l.append(bin_)
    ? ? ? ? for i in ''.join(l).split('0')[1:]:
    ? ? ? ? ? ? if len(i) > 0:
    ? ? ? ? ? ? ? ? return False
    ? ? ? ? return True
    ? ? else:
    ? ? ? ? return False


    import sys
    a, b, c, d, e, err_, pri_ = 0, 0, 0, 0, 0, 0, 0
    while 1:
    ? ? try:
    ? ? ? ? string = sys.stdin.readline().strip().split()
    ? ? ? ? s = string[0].split('~')
    ? ? ? ? if ip4_check(s[0]) and subnet_mask(s[1]):
    ? ? ? ? ? ? ip_0 = int(s[0].split('.')[0])
    ? ? ? ? ? ? if 1 <= ip_0 <= 126:
    ? ? ? ? ? ? ? ? a += 1
    ? ? ? ? ? ? ? ? # print(s)
    ? ? ? ? ? ? ? ? if ip_0 == 10:
    ? ? ? ? ? ? ? ? ? ? pri_ += 1
    ? ? ? ? ? ? elif 128 <= ip_0 <= 191:
    ? ? ? ? ? ? ? ? b += 1
    ? ? ? ? ? ? ? ? if ip_0 == 172 and 16 <= int(s[0].split('.')[1]) <= 31:
    ? ? ? ? ? ? ? ? ? ? pri_ += 1
    ? ? ? ? ? ? elif 192 <= ip_0 <= 223:
    ? ? ? ? ? ? ? ? c += 1
    ? ? ? ? ? ? ? ? if ip_0 == 192 and int(s[0].split('.')[1]) == 168:
    ? ? ? ? ? ? ? ? ? ? pri_ += 1
    ? ? ? ? ? ? elif 224 <= ip_0 <= 239:
    ? ? ? ? ? ? ? ? d += 1
    ? ? ? ? ? ? elif 240 <= ip_0 <= 255:
    ? ? ? ? ? ? ? ? e += 1
    ? ? ? ? ? ? elif ip_0 == 0 or ip_0 == 127:
    ? ? ? ? ? ? ? ? continue
    ? ? ? ? ? ? else:
    ? ? ? ? ? ? ? ? err_ += 1
    ? ? ? ? ? ? ? ? continue
    ? ? ? ? else:
    ? ? ? ? ? ? err_ += 1
    ? ? ? ? ? ? continue
    ? ? except:
    ? ? ? ? break

    print(' '.join([str(a), str(b), str(c), str(d), str(e), str(err_), str(pri_)]))

    ?

    cs