Exemplo n.º 1
0
 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
Exemplo n.º 2
0
 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)
Exemplo n.º 3
0
 def test_get_host_max(self):
     cidr = Cidr()
     hostmax = cidr.get_host_max(cidr.get_broadcast(cidr.net_to_bin('192.168.16.100'), cidr.mask_to_bin(22)))
     ip = cidr.binary_to_ip(hostmax)
     self.assertEqual(ip, '192.168.19.254')
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
 def test_get_broadcast(self):
     cidr = Cidr()
     broadcast = cidr.get_broadcast(cidr.net_to_bin('192.168.16.100'), cidr.mask_to_bin(22))
     result = int(''.join(map(str, broadcast)))
     self.assertEqual(result, 11000000101010000001001111111111)