def start(self): if self.cidr_prefix_length is not None: start, end = ipu.cidr_to_range(self.first_ip, self.cidr_prefix_length) return start else: return ipu.to_number(self.first_ip)
def end(self): if self.last_ip is not None: return ipu.to_number(self.last_ip) if self.cidr_prefix_length is not None: start, end = ipu.cidr_to_range(self.first_ip, self.cidr_prefix_length) return end return self.start
def test_ip_to_number_conversions_ipv6(self): self.assertEqual(1, ipu.to_number('0:0:0:0:0:0:0:1')) self.assertEqual(10, ipu.to_number('0:0:0:0:0:0:0:a')) self.assertEqual((2 ** 16) ** 7 + 10, ipu.to_number('1:0:0:0:0:0:0:a')) # Zero collapse syntax self.assertEqual(0, ipu.to_number('::')) self.assertEqual(1, ipu.to_number('::1')) self.assertEqual((2 ** 16) ** 7 + 10, ipu.to_number('1::a')) # Mixed syntax self.assertEqual(1, ipu.to_number('::0.0.0.1')) self.assertEqual((2 ** 16) ** 7 + 10, ipu.to_number('1::0.0.0.10'))
def __contains__(self, ip): ip_nr = ipu.to_number(ip) return self.start <= ip_nr <= self.end
def test_ip_to_number_conversions(self): self.assertEqual(0, ipu.to_number('0.0.0.0')) self.assertEqual(1, ipu.to_number('0.0.0.1')) self.assertEqual(256, ipu.to_number('0.0.1.0')) self.assertEqual(65537, ipu.to_number('0.1.0.1')) self.assertEqual(16842753, ipu.to_number('1.1.0.1'))
def test_ip_to_number_conversions_ipv6(self): self.assertEqual(0, ipu.to_number('::'))