def cidr_handler(self, message): net, mask = message.split('/')[0], message.split('/')[1] if int(mask) not in range(0, 32): raise ValueError('маска не входит допустимый диапазон') response = self.cache.get(message) if response is None: cidr = Cidr() binary_subnet = cidr.get_subnet(cidr.net_to_bin(net), cidr.mask_to_bin(int(mask))) subnet = cidr.binary_to_ip(binary_subnet) binary_broadcast = cidr.get_broadcast(cidr.net_to_bin(net), cidr.mask_to_bin(int(mask))) broadcast = cidr.binary_to_ip(binary_broadcast) host_min = cidr.binary_to_ip(cidr.get_host_min(binary_subnet)) host_max = cidr.binary_to_ip(cidr.get_host_max(binary_broadcast)) host_count = cidr.get_host_count(int(mask)) response = "network - {0} \r\n " \ "broadcast - {1} \r\n " \ "hostmin - {2} \r\n " \ "hostmax - {3} \r\n " \ "hosts - {4} \r\n".format(subnet, broadcast, host_min, host_max, host_count) self.cache.set(message, response) return response
def test_get_host_count(self): cidr = Cidr() host_count = cidr.get_host_count(22) self.assertEqual(host_count, 1022)