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_network_by_range(self): cidr = Cidr() hostmin = cidr.get_host_min(cidr.get_subnet(cidr.net_to_bin('192.168.16.100'), cidr.mask_to_bin(22))) hostmax = cidr.get_host_max(cidr.get_broadcast(cidr.net_to_bin('192.168.16.100'), cidr.mask_to_bin(22))) network = cidr.get_network_by_range(hostmin, hostmax) result = int(''.join(map(str, network))) self.assertEqual(result, 11000000101010000001000000000000)
def test_get_host_min(self): cidr = Cidr() hostmin = cidr.get_host_min(cidr.get_subnet(cidr.net_to_bin('192.168.16.100'), cidr.mask_to_bin(22))) ip = cidr.binary_to_ip(hostmin) self.assertEqual(ip, '192.168.16.1')
def test_get_mask_by_range(self): cidr = Cidr() hostmin = cidr.get_host_min(cidr.get_subnet(cidr.net_to_bin('192.168.16.100'), cidr.mask_to_bin(22))) hostmax = cidr.get_host_max(cidr.get_broadcast(cidr.net_to_bin('192.168.16.100'), cidr.mask_to_bin(22))) mask = cidr.get_mask_by_range(hostmin, hostmax) self.assertEqual(mask, 22)
def test_get_subnet(self): cidr = Cidr() network = cidr.get_subnet(cidr.net_to_bin('192.168.16.100'), cidr.mask_to_bin(22)) result = int(''.join(map(str, network))) self.assertEqual(result, 11000000101010000001000000000000)