def update_info(self, result=None): """ Update the information of the subnet. If result is omitted, a new request is sent to the robot to gather the information. """ if result is None: result = self.conn.get('/subnet/{0}'.format(self.net_ip)) data = result['subnet'] self.net_ip = data['ip'] self.mask = data['mask'] self.gateway = data['gateway'] self.server_ip = data['server_ip'] self.failover = data['failover'] self.locked = data['locked'] self.traffic_warnings = data['traffic_warnings'] self.traffic_hourly = data['traffic_hourly'] self.traffic_daily = data['traffic_daily'] self.traffic_monthly = data['traffic_monthly'] self.is_ipv6, self.numeric_net_ip = addr.parse_ipaddr(self.net_ip) self.numeric_gateway = addr.parse_ipaddr(self.gateway, self.is_ipv6) getrange = addr.get_ipv6_range if self.is_ipv6 else addr.get_ipv4_range self.numeric_range = getrange(self.numeric_net_ip, self.mask)
def test_parse_ipaddr(self): self.assertEqual(parse_ipaddr('1.2.3.4'), (False, 16909060)) self.assertEqual(parse_ipaddr('255.255.0.0', False), 4294901760) self.assertEqual(parse_ipaddr('dead::beef'), (True, 295986882420777848964380943247191621359)) self.assertEqual(parse_ipaddr('ffff::ffff', True), 340277174624079928635746076935439056895) self.assertRaises(socket.error, parse_ipaddr, '1.2.3.4', True) self.assertRaises(socket.error, parse_ipaddr, 'dead::beef', False) self.assertRaises(socket.error, parse_ipaddr, 'invalid')
def __contains__(self, addr): """ Check whether a specific IP address is within the current subnet. """ numeric_addr = addr.parse_ipaddr(addr, self.is_ipv6) return self.numeric_range[0] <= numeric_addr <= self.numeric_range[1]