How to use netaddr - 10 common examples

To help you get started, we’ve selected a few netaddr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jathanism / cidrize / tests / test_cidrize.py View on Github external
def test_hyphen_style_loose_toobig(self):
        # IPRange objects larger than /18 will always be strict.
        expected = [IPNetwork('10.0.0.0/18'), IPNetwork('10.0.64.0/29')]
        _input = '10.0.0.0-10.0.64.7'
        self.assertEqual(expected, self.test(_input, strict=False))
github drkjam / netaddr / test / ip / test_ip_v4.py View on Github external
def test_ipnetwork_index_operations_v4():
    ip = IPNetwork('192.0.2.16/29')
    assert ip[0] == IPAddress('192.0.2.16')
    assert ip[1] == IPAddress('192.0.2.17')
    assert ip[-1] == IPAddress('192.0.2.23')
github drkjam / netaddr / test / ip / test_platform_osx.py View on Github external
def test_ip_behaviour_osx():
    assert iprange_to_cidrs('::1', '::255.255.255.254') == [
        IPNetwork('::1/128'),
        IPNetwork('::0.0.0.2/127'),
        IPNetwork('::0.0.0.4/126'),
        IPNetwork('::0.0.0.8/125'),
        IPNetwork('::0.0.0.16/124'),
        IPNetwork('::0.0.0.32/123'),
        IPNetwork('::0.0.0.64/122'),
        IPNetwork('::0.0.0.128/121'),
        IPNetwork('::0.0.1.0/120'),
        IPNetwork('::0.0.2.0/119'),
        IPNetwork('::0.0.4.0/118'),
        IPNetwork('::0.0.8.0/117'),
        IPNetwork('::0.0.16.0/116'),
        IPNetwork('::0.0.32.0/115'),
        IPNetwork('::0.0.64.0/114'),
        IPNetwork('::0.0.128.0/113'),
        IPNetwork('::0.1.0.0/112'),
        IPNetwork('::0.2.0.0/111'),
        IPNetwork('::0.4.0.0/110'),
        IPNetwork('::0.8.0.0/109'),
        IPNetwork('::0.16.0.0/108'),
        IPNetwork('::0.32.0.0/107'),
        IPNetwork('::0.64.0.0/106'),
        IPNetwork('::0.128.0.0/105'),
        IPNetwork('::1.0.0.0/104'),
        IPNetwork('::2.0.0.0/103'),
        IPNetwork('::4.0.0.0/102'),
        IPNetwork('::8.0.0.0/101'),
        IPNetwork('::16.0.0.0/100'),
github drkjam / netaddr / test / ip / test_platform_osx.py View on Github external
IPNetwork('::4/126'),
        IPNetwork('::8/125'),
        IPNetwork('::10/124'),
        IPNetwork('::20/123'),
        IPNetwork('::40/122'),
        IPNetwork('::80/121'),
        IPNetwork('::100/120'),
        IPNetwork('::200/119'),
        IPNetwork('::400/118'),
        IPNetwork('::800/117'),
        IPNetwork('::1000/116'),
        IPNetwork('::2000/115'),
        IPNetwork('::4000/114'),
        IPNetwork('::8000/113'),
        IPNetwork('::0.1.0.0/112'),
        IPNetwork('::0.2.0.0/111'),
        IPNetwork('::0.4.0.0/110'),
        IPNetwork('::0.8.0.0/109'),
        IPNetwork('::0.16.0.0/108'),
        IPNetwork('::0.32.0.0/107'),
        IPNetwork('::0.64.0.0/106'),
        IPNetwork('::0.128.0.0/105'),
        IPNetwork('::1.0.0.0/104'),
        IPNetwork('::2.0.0.0/103'),
        IPNetwork('::4.0.0.0/102'),
        IPNetwork('::8.0.0.0/101'),
        IPNetwork('::16.0.0.0/100'),
        IPNetwork('::32.0.0.0/99'),
        IPNetwork('::64.0.0.0/98'),
        IPNetwork('::128.0.0.0/98'),
        IPNetwork('::192.0.0.0/99'),
        IPNetwork('::224.0.0.0/100'),
github drkjam / netaddr / test / ip / test_network_ops.py View on Github external
def test_subnetting():
    ip = IPNetwork('172.24.0.0/23')
    assert isinstance(ip.subnet(28), types.GeneratorType)

    subnets = list(ip.subnet(28))
    assert len(subnets) == 32

    assert subnets == [
        IPNetwork('172.24.0.0/28'),
        IPNetwork('172.24.0.16/28'),
        IPNetwork('172.24.0.32/28'),
        IPNetwork('172.24.0.48/28'),
        IPNetwork('172.24.0.64/28'),
        IPNetwork('172.24.0.80/28'),
        IPNetwork('172.24.0.96/28'),
        IPNetwork('172.24.0.112/28'),
        IPNetwork('172.24.0.128/28'),
        IPNetwork('172.24.0.144/28'),
github drkjam / netaddr / test / ip / test_ip_v4.py View on Github external
def test_ipaddress_integer_operations_v4():
    assert IPAddress('192.0.2.0') + 1 == IPAddress('192.0.2.1')
    assert 1 + IPAddress('192.0.2.0') == IPAddress('192.0.2.1')
    assert IPAddress('192.0.2.1') - 1 == IPAddress('192.0.2.0')
    assert IPAddress('192.0.0.0') + IPAddress('0.0.0.42') == IPAddress('192.0.0.42')
    assert IPAddress('192.0.0.42') - IPAddress('0.0.0.42') == IPAddress('192.0.0.0')

    with pytest.raises(IndexError):
        1 - IPAddress('192.0.2.1')

    ip = IPAddress('10.0.0.1')
    ip += 1
    assert ip == IPAddress('10.0.0.2')

    ip -= 1
    assert ip == IPAddress('10.0.0.1')

    ip += IPAddress('0.0.0.42')
    assert ip == IPAddress('10.0.0.43')
github drkjam / netaddr / test / ip / test_ip_sets.py View on Github external
def test_ipset_supersets_and_subsets():
    s1 = IPSet(['192.0.2.0/24', '192.0.4.0/24'])
    s2 = IPSet(['192.0.2.0', '192.0.4.0'])

    assert s1.issuperset(s2)
    assert s2.issubset(s1)
    assert not s2.issuperset(s1)
    assert not s1.issubset(s2)

    ipv4_addr_space = IPSet(['0.0.0.0/0'])
    private = IPSet(['10.0.0.0/8', '172.16.0.0/12', '192.0.2.0/24',
                     '192.168.0.0/16', '239.192.0.0/14'])
    reserved = IPSet(['225.0.0.0/8', '226.0.0.0/7', '228.0.0.0/6', '234.0.0.0/7',
                      '236.0.0.0/7', '238.0.0.0/8', '240.0.0.0/4'])
    unavailable = reserved | private
    available = ipv4_addr_space ^ unavailable

    assert [tuple(map(str, (cidr, cidr[0], cidr[-1]))) for cidr in available.iter_cidrs()] == [
        ('0.0.0.0/5', '0.0.0.0', '7.255.255.255'),
github drkjam / netaddr / test / ip / test_ip_sets.py View on Github external
def test_ipset_comparison():
    s1 = IPSet(['fc00::/2'])
    s2 = IPSet(['fc00::/3'])

    assert s1 > s2
    assert not s1 < s2
    assert s1 != s2
github drkjam / netaddr / test / ip / test_ip_sets.py View on Github external
def test_ipset_member_insertion_and_deletion():
    s1 = IPSet()
    s1.add('192.0.2.0')
    assert s1 == IPSet(['192.0.2.0/32'])

    s1.remove('192.0.2.0')
    assert s1 == IPSet([])

    s1.add(IPRange("10.0.0.0", "10.0.0.255"))
    assert s1 == IPSet(['10.0.0.0/24'])

    s1.remove(IPRange("10.0.0.128", "10.10.10.10"))
    assert s1 == IPSet(['10.0.0.0/25'])
github beaker-project / beaker / IntegrationTests / src / bkr / inttest / server / test_model.py View on Github external
def test_lowest_free_mac_gap_at_start(self):
        first_job = data_setup.create_job(num_guestrecipes=1)
        data_setup.mark_job_running(first_job)
        self.assertEquals(first_job.recipesets[0].recipes[0].guests[0].resource.mac_address,
                    netaddr.EUI('52:54:00:00:00:00'))
        second_job = data_setup.create_job(num_guestrecipes=1)
        data_setup.mark_job_running(second_job)
        self.assertEquals(second_job.recipesets[0].recipes[0].guests[0].resource.mac_address,
                    netaddr.EUI('52:54:00:00:00:01'))
        self.assertEquals(RecipeResource._lowest_free_mac(),
                    netaddr.EUI('52:54:00:00:00:02'))
        first_job.cancel()
        first_job.update_status()
        self.assertEquals(RecipeResource._lowest_free_mac(),
                    netaddr.EUI('52:54:00:00:00:00'))