识别有效的IP地址和掩码并进行分类统计

import re, sys

'''
思路是:
1. 定义res数组记录后面的判定结果
2. 先把IP地址串转化成int值,如 '255.0.0.0' -> 11111111000000000000000000000000 -> 4278190080 (其实可以不用转为int)
3. 将IP地址归类一一判定:
    1) ip地址和掩码地址合法性检查。注意0.0.0.0 和 255.255.255.255都是非法的
    2) A/B/C/D/E类判定, 要注意A/B/C类中还含有私有网络地址,另需要加上127.0.0.0-127.255.255.255的类别判定
4. 最后打印记录值res
'''

res = [0] * 7  # 结果集
               # 统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数

def ipPartition(ip, mask):
    ip_bin_str = ip_to_bin_str(ip)
    mask_bin_str = ip_to_bin_str(mask)
    if not mask_bin_str \
            or mask == "0.0.0.0" \
            or mask == "255.255.255.255" \
            or re.search(r'1["0"]+1', mask_bin_str):  # 掩码地址检查
        res[5] += 1
        return None

    if ip_bin_str:
        if ip_bin_str >= ip_to_bin_str('1.0.0.0') and ip_bin_str <= ip_to_bin_str('126.255.255.255'):
            if ip_bin_str >= ip_to_bin_str('10.0.0.0') and ip_bin_str <= ip_to_bin_str('10.255.255.255'):
                res[-1] += 1
            res[0] += 1
        elif ip_bin_str >= ip_to_bin_str('127.0.0.0') and ip_bin_str <= ip_to_bin_str('127.255.255.255'):
            pass
        elif ip_bin_str >= ip_to_bin_str('128.0.0.0') and ip_bin_str <= ip_to_bin_str('191.255.255.255'):
            if ip_bin_str >= ip_to_bin_str('172.0.0.0') and ip_bin_str <= ip_to_bin_str('172.31.255.255'):
                res[-1] += 1
            res[1] += 1
        elif ip_bin_str >= ip_to_bin_str('192.0.0.0') and ip_bin_str <= ip_to_bin_str('223.255.255.255'):
            if ip_bin_str >= ip_to_bin_str('192.168.0.0') and ip_bin_str <= ip_to_bin_str('192.168.255.255'):
                res[-1] += 1
            res[2] += 1
        elif ip_bin_str >= ip_to_bin_str('224.0.0.0') and ip_bin_str <= ip_to_bin_str('239.255.255.255'):
            res[3] += 1
        elif ip_bin_str >= ip_to_bin_str('240.0.0.0') and ip_bin_str <= ip_to_bin_str('255.255.255.255'):
            res[4] += 1
    else:
        res[5] += 1


def ip_to_bin_str(ip):
    if not re.search(r"^\d+\.\d+\.\d+\.\d+$", ip):  # ip地址格式检查
        return None

    # 对ip进行二进制转换
    ip_bin_str = ""
    for i in ip.split("."):
        tmp = str(bin(int(i)))[2:]
        tmp = "".join((8 - len(tmp)) * ['0']) + tmp
        ip_bin_str = ip_bin_str + tmp

    return ip_bin_str


if __name__ == "__main__":
    try:
        for line in sys.stdin:
            ip, mask = line.strip().split("~")
            ipPartition(ip, mask)

        print(" ".join([str(i) for i in res]))
    except Exception as e:
        #print(e)
        pass;
题目描述

 请解析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.*.*.*】和【127.*.*.*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时可以忽略    
2. 私有IP地址和A,B,C,D,E类地址是不冲突的
3. 子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码)    注意二进制下全是1或者全是0均为非法    
    
 
输入描述:
多行字符串。每行一个IP地址和掩码,用~隔开。
输出描述:
统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。

示例:

输入
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
示例2:
237.143.207.113~255.0.0.0
0.154.70.56~255.0.0.0
47.191.88.206~255.255.0.0
252.117.65.51~255.0.0.0
5.127.233.251~255.255.0.0
105.2.227.18~255.0.0.0
42.243.99.190~255.255.143.0
213.136.4.7~255.0.0.0
18.215.41.233~255.255.255.67
193.53.72.97~255.255.0.0
231.25.34.159~255.255.0.0
62.38.160.21~255.78.255.0
193.7.199.159~255.255.0.0
213.188.214.148~255.0.0.0
123.39.218.193~255.178.0.0
240.246.182.37~255.0.0.0
45.217.35.228~255.0.0.0
62.97.41.64~255.0.0.0
210.175.136.234~255.0.0.0
82.198.156.228~255.0.0.0
243.72.65.231~255.0.0.0
95.73.56.172~255.0.0.0
216.124.139.160~255.255.255.0
158.63.191.33~255.255.255.0
170.230.29.86~255.255.120.255
115.232.165.231~255.255.255.255
232.118.3.82~255.28.35.255
12.226.20.210~255.236.0.0
67.76.187.160~255.255.255.0
53.76.14.193~255.0.0.0
80.237.42.83~255.0.0.0
225.2.60.197~255.6.255.13
165.141.251.150~255.255.0.0
172.75.97.143~255.0.0.0
198.166.79.85~255.255.0.0
228.175.133.241~255.255.0.0
70.176.238.139~255.0.0.0
54.67.162.54~255.45.255.120
38.152.223.23~255.0.0.0
129.152.202.62~255.0.0.0
221.9.212.214~255.0.132.0
153.148.236.124~255.255.255.0
188.36.36.43~255.255.0.0
244.155.142.53~255.255.255.0
246.80.203.40~255.255.0.0
124.112.218.29~255.255.0.0
111.209.161.129~255.255.255.0
35.116.236.164~255.255.255.32
179.94.121.97~255.0.0.0
99.96.129.12~255.255.255.255
50.227.105.219~255.0.0.0
29.242.7.49~255.0.0.0
139.31.65.107~255.255.0.0
72.50.111.118~255.255.255.0
193.242.64.7~255.255.0.0
254.56.183.232~255.255.0.0
12.179.234.92~255.255.255.255
17.95.176.188~255.0.0.0
89.209.212.195~255.255.0.0
132.59.5.10~255.255.0.0
82.157.198.52~255.0.0.0
6.72.161.12~255.252.0.0
47.178.94.149~255.255.0.0
147.243.229.30~255.255.255.0
242.18.226.176~255.0.0.0
187.72.221.156~255.0.0.0
74.136.48.91~255.255.255.0
194.161.244.213~255.0.0.0
176.231.190.78~255.255.0.0
223.164.187.227~255.0.0.0
251.47.51.76~255.132.255.0
90.108.60.242~255.255.255.255
163.170.22.99~255.255.255.0
229.78.165.75~255.255.0.0
54.222.173.56~255.255.0.0
190.90.6.221~255.0.0.0
79.60.73.212~255.0.240.0
198.137.223.142~255.0.0.0
19.220.60.252~255.255.255.255
84.109.38.202~255.255.202.0
189.228.141.30~255.255.0.0
114.9.145.171~255.0.0.0
53.208.55.234~255.255.255.0
160.172.82.18~255.255.255.0
51.56.219.106~255.255.0.0
190.207.66.172~255.255.0.0
230.58.1.224~255.255.0.0
64.150.187.135~255.6.255.255
235.40.32.49~255.255.0.0
73.154.128.199~255.0.0.0
58.120.129.127~255.0.44.0
43.15.83.156~255.220.0.0
41.71.211.168~255.255.255.255
163.90.16.26~255.0.0.0
91.199.55.68~255.255.0.0
148.125.152.107~255.0.0.0
15.158.22.221~255.255.255.255
113.200.126.58~255.0.0.0
222.138.60.69~255.255.0.0
224.109.93.237~255.16.0.0
100.79.107.3~255.0.0.0
28.8.87.106~255.255.0.0
35.6.154.226~255.64.0.0
94.66.198.42~255.255.255.0
138.91.63.51~255.255.255.255
79.163.185.212~255.0.73.0
13.47.62.254~255.255.0.0
0.98.251.80~255.255.0.0
64.202.210.194~255.109.0.0
14.40.186.199~255.0.0.0
40.147.20.10~255.130.255.255
42.96.80.216~255.255.0.0
40.209.106.107~255.0.0.44
1.4.190.37~255.0.0.0
90.84.140.176~255.0.0.0
60.160.75.176~255.0.116.0
224.13.12.33~255.0.0.0
121.183.63.101~255.255.0.0
69.251.16.86~255.255.0.0
189.249.195.152~255.255.0.0
214.195.53.224~255.255.255.0
147.92.127.195~255.20.255.255
91.228.181.229~255.0.0.0
133.132.202.183~255.0.0.194
145.99.161.183~255.0.0.0
146.23.46.148~255.255.255.0
53.169.214.204~255.0.0.0
254.188.234.67~255.0.0.0
45.150.148.129~255.121.0.0
116.50.190.222~255.0.0.0
28.132.153.169~255.0.21.0
142.135.130.154~255.0.0.0
159.142.190.4~255.255.0.50
128.6.204.108~255.255.255.255
169.208.53.137~255.255.0.0
91.87.203.152~255.0.0.0
18.106.204.42~255.0.0.0
135.188.203.11~255.38.255.255
212.143.77.99~255.0.0.0
125.156.175.175~255.0.0.0
16.91.71.194~255.255.0.0
188.93.180.198~255.0.35.0
225.199.61.74~255.0.0.0
208.137.66.197~255.250.0.0
182.59.211.127~255.255.255.255
151.114.181.6~255.255.0.0
137.171.116.236~255.255.255.0
49.16.145.71~255.0.0.0
132.125.51.0~255.0.0.0
65.181.121.61~255.255.0.0
93.67.229.191~255.255.255.255
176.131.218.176~255.43.0.0
116.19.9.54~255.255.0.0
120.2.106.116~255.0.0.0
63.121.49.129~255.0.178.0
101.164.139.247~255.0.0.151
87.240.168.249~255.0.0.0
86.13.245.114~255.0.0.0
166.153.184.220~255.255.0.0
140.192.94.128~255.255.255.0
234.137.5.101~255.0.0.0
14.67.45.3~255.255.255.103
139.22.95.184~255.255.255.0
84.167.82.117~255.255.255.0
146.81.247.172~255.255.255.0
43.61.101.60~255.255.255.37
226.181.198.45~255.0.0.0
86.183.134.176~255.0.110.0
172.187.201.37~255.255.255.0
78.92.195.189~255.255.255.58
49.166.53.50~255.255.255.255
217.49.211.121~255.0.0.0
79.247.26.197~255.0.0.0
41.107.42.174~255.0.0.30
245.215.75.184~255.255.0.0
207.2.79.93~255.0.0.0
137.180.128.248~255.255.0.0
218.131.220.8~255.0.0.0
63.81.15.106~255.255.255.0
117.199.49.82~255.0.0.0
72.32.205.233~255.0.0.0
60.102.39.147~255.229.255.0
141.13.125.188~255.0.205.0
192.70.108.29~255.0.0.0
50.110.229.178~255.255.0.63
168.184.56.244~255.167.255.0
183.229.153.179~255.255.0.0
142.11.227.97~255.255.255.255
128.230.188.51~255.0.0.0
115.115.173.90~255.255.255.0
0.225.159.70~255.255.0.0
148.224.29.42~255.255.0.0
253.60.17.23~255.255.255.0
97.129.31.87~255.0.0.0
145.206.229.167~255.0.0.0
179.178.201.165~255.0.0.0
60.6.47.94~255.255.0.0
97.42.90.152~255.158.255.255
219.59.196.192~255.255.0.0
228.16.173.236~255.255.0.232
238.124.61.67~255.0.0.0
148.223.212.228~255.0.193.0
198.177.212.136~255.255.119.255
67.172.177.72~255.0.0.0
82.54.109.193~255.255.0.0
170.75.209.121~255.0.0.0
241.102.6.161~255.255.255.255
107.164.39.154~255.255.255.255
10.208.59.135~255.255.0.0
160.90.191.172~255.255.0.0
218.92.24.2~255.0.0.0
22.221.180.118~255.0.0.0
136.191.32.169~255.0.203.0
89.29.188.169~255.0.0.0
119.179.89.216~255.255.255.255
117.14.180.194~255.0.0.0
12.152.112.19~255.131.255.0
113.12.58.159~255.255.0.0
216.28.65.9~255.255.0.0
155.57.46.56~255.74.255.0
116.88.236.253~255.0.0.0
62.171.35.217~255.0.0.0
19.13.187.141~255.0.0.0
144.166.52.157~255.5.255.255
234.193.80.85~255.255.0.0
179.181.205.29~255.0.0.0

对应输出应该为:

71 41 21 12 10 68 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IP地址是网络中给每台计算机设备分配的唯一标识符,用于在互联网上进行通信。IP地址由32位二进制数组成,通常以四组十进制数表示,例如:192.168.0.1。为了方便管理和分类IP地址被分为四类,分别是A、B、C、D、E类。不同类别的IP地址有不同的网络前缀,也就是划分为不同的网络。 - A类地址:1.0.0.0至126.0.0.0,第一位为0,网络号占用8位,主机号占用24位。 - B类地址:128.0.0.0至191.255.0.0,第一位为10,网络号占用16位,主机号占用16位。 - C类地址:192.0.0.0至223.255.255.0,第一位为110,网络号占用24位,主机号占用8位。 - D类地址:224.0.0.0至239.255.255.255,第一位为1110,用于多播。 - E类地址:240.0.0.0至247.255.255.255,第一位为1111,保留地址。 当一个网络需要分成多个子网络时,就需要使用子网掩码。子网掩码也是32位的二进制数,它由一段1和一段0组成,与IP地址进行按位与运算后,得到的结果就是网络号和子网号。子网掩码分A、B、C三类,分别如下: - A类地址子网掩码:255.0.0.0 - B类地址子网掩码:255.255.0.0 - C类地址子网掩码:255.255.255.0 在实际网络中,为了最大限度地利用IP地址,通常会将网络号和主机号进一步划分为子网号和主机号,以方便管理和分配。比如,一个B类地址可以被划分为多个子网,其中前16位为网络号,中间8位为子网号,最后8位为主机号。子网掩码也可以根据实际需求进行调整,以满足具体的网络需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值