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 range_handler(self, message): hostmin, hostmax = message.split('-')[0].strip(), message.split('-')[1].strip() response = self.cache.get(message) if response is None: cidr = Cidr() binary_hostmin = cidr.get_host_min(cidr.net_to_bin(hostmin)) binary_hostmax = cidr.get_host_max(cidr.net_to_bin(hostmax)) mask = cidr.get_mask_by_range(binary_hostmin, binary_hostmax) network = cidr.binary_to_ip(cidr.get_network_by_range(binary_hostmin, binary_hostmax)) response = "{0}/{1}".format(network, mask) self.cache.set(message, response) return response