1.先剔除0及127开头的IP地址 2.判断掩码及IP规则是否符合要求 3.判断各类IP地址
from sys import stdin
from re import match
from functools import reduce
def judgef(num): #将IP中的空值或者小于0及大于255的值统一赋值用以后续的IP合规判断
if num == '':
return 300
if int(num) >= 0 and int(num) <= 255:
return int(num)
else:
return 300
def dealmask(omask1,omask2): #将掩码值处理为二进制01字符串
res = omask1+bin(omask2)[2:].zfill(8)
return res
def decidef(olist): #将IP及掩码进行分类
if olist[0] == 0 or olist[0] == 127: #将0.*.*.*及127.*.*.*IP剔除
return
omasks = reduce(dealmask,olist[4:],'')
if not (omasks.count('10') == 1 and omasks.count('01') == 0) :
#判断掩码是否错误,只存在一个10 且不存在01 的掩码符合规则
result[5] += 1
return
if 300 in olist[:4]: #判断IP是否错误
result[5] += 1
return
if 1 <= olist[0] <= 126 and 0 <= olist[1] <= 255 and 0 <= olist[2] <= 255 and 0 <= olist[3] <= 255: #判断是否为A类
result[0] += 1
if 128 <= olist[0] <= 191 and 0 <= olist[1] <= 255 and 0 <= olist[2] <= 255 and 0 <= olist[3] <= 255:#判断是否为B类
result[1] += 1
if 192 <= olist[0] <= 223 and 0 <= olist[1] <= 255 and 0 <= olist[2] <= 255 and 0 <= olist[3] <= 255:#判断是否为C类
result[2] += 1
if 224 <= olist[0] <= 239 and 0 <= olist[1] <= 255 and 0 <= olist[2] <= 255 and 0 <= olist[3] <= 255:#判断是否为D类
result[3] += 1
if 240 <= olist[0] <= 255 and 0 <= olist[1] <= 255 and 0 <= olist[2] <= 255 and 0 <= olist[3] <= 255:#判断是否为E类
result[4] += 1
if (olist[0] == 10 and 0 <= olist[1] <= 255 and 0 <= olist[2] <= 255 and 0 <= olist[3] <= 255) \
or (olist[0] == 172 and 16 <= olist[1] <= 31 and 0 <= olist[2] <= 255 and 0 <= olist[3] <= 255 ) \
or (olist[0] == 192 and olist[1] == 168 and 0 <= olist[2] <= 255 and 0 <= olist[3] <= 255 ) \
: #判断是否为私有IP
result[6] += 1
result = [0,0,0,0,0,0,0]
for ostr in stdin.readlines(): #初步处理输入字符
deals = list(map(judgef,ostr.strip().replace('~','.').split('.')))
decidef(deals)
for i in result:
print(i,end=' ')