class IpMask:
def __init__(self, ip_mask):
self.ip_mask = ip_mask.strip()
self.d1 = dict.fromkeys('abcdefg', 0)
def __ip(self):
"""返回ip地址"""
return self.ip_mask.split("~")[0]
def __mask(self):
"""返回子网掩码"""
return self.ip_mask.split("~")[1]
@staticmethod
def __convert_bin(im):
"""返回二进制格式"""
if im is not None:
im_bin = bin(int(im))[2:]
if len(im_bin) < 8:
im_bin = '0' * (8 - len(im_bin)) + im_bin
return im_bin
def validate_mask(self):
"""验证子网掩码有效性
mask_bin: 二进制的子网掩码
"""
mask_bin = "".join([self.__convert_bin(im) for im in self.__mask().split(".")])
if "0" not in mask_bin or "1" not in mask_bin or "1" in mask_bin[mask_bin.find('0')+1:]:
self.d1['f'] += 1
return False
return True
def validate_ip(self):
"""错误ip"""
ip1 = self.__ip()
p1, *num = ip1.split(".")
# 每一位必须存在,不能为空;首位必须在1-255之间,其余的在0到255之间
t = list(map(lambda x: len(x) < 1, ip1.split(".")))
if p1 == '127' or p1 == '0':
return False
else:
if any(t) or not 1 <= int(p1) <= 255 or not all([True for i in num if 0 <= int(i) <= 255]):
"""无效的ip地址"""
self.d1['f'] += 1
return False
return True
def ip_classify(self):
"""给有效的ip地址分类,无效的归类计数"""
if self.validate_ip() and self.validate_mask():
ip2 = self.__ip()
p1, *num1 = ip2.split(".")
p1_bin = self.__convert_bin(p1)
if p1_bin.startswith('1111'):
self.d1['e'] += 1
elif p1_bin.startswith('111'):
self.d1['d'] += 1
elif p1_bin.startswith('110'):
self.d1['c'] += 1
if ip2.startswith('192.168'):
self.d1['g'] += 1
elif p1_bin.startswith('10'):
self.d1['b'] += 1
if ip2.startswith('172') and 16 <= int(num1[0]) <= 31:
self.d1['g'] += 1
elif p1_bin.startswith('0'):
self.d1['a'] += 1
if p1 == '10':
self.d1['g'] += 1
import sys
dd = {}
for line in sys.stdin:
ip_mask = line.strip()
ipm = IpMask(ip_mask)
ipm.ip_classify()
for key, value in ipm.d1.items():
if key in dd:
dd[key] += value
else:
dd[key] = value
for key, value in dd.items():
print(value,end=" ")