Adding network scanning
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
=Python 2.x and 3.x compatibility tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.compat import _sys_maxint, _is_str, _is_int, _callable
|
||||
|
||||
>>> from netaddr.compat import _func_doc, _dict_keys, _dict_items
|
||||
|
||||
>>> from netaddr.compat import _iter_dict_keys, _bytes_join, _zip, _range
|
||||
|
||||
>>> from netaddr.compat import _iter_range, _func_name, _func_doc
|
||||
|
||||
# string and integer detection tests.
|
||||
>>> _is_int(_sys_maxint)
|
||||
True
|
||||
|
||||
>>> _is_str(_sys_maxint)
|
||||
False
|
||||
|
||||
>>> _is_str('')
|
||||
True
|
||||
|
||||
>>> _is_str(''.encode())
|
||||
True
|
||||
|
||||
>>> _is_str(unicode(''))
|
||||
True
|
||||
|
||||
# Python 2.x - 8 bit strings are just regular strings
|
||||
>>> str_8bit = _bytes_join(['a', 'b', 'c'])
|
||||
|
||||
>>> str_8bit == 'abc'.encode()
|
||||
True
|
||||
|
||||
>>> "'abc'" == '%r' % str_8bit
|
||||
True
|
||||
|
||||
# dict operation tests.
|
||||
>>> d = { 'a' : 0, 'b' : 1, 'c' : 2 }
|
||||
|
||||
>>> sorted(_dict_keys(d)) == ['a', 'b', 'c']
|
||||
True
|
||||
|
||||
>>> sorted(_dict_items(d)) == [('a', 0), ('b', 1), ('c', 2)]
|
||||
True
|
||||
|
||||
# zip() BIF tests.
|
||||
>>> l2 = _zip([0], [1])
|
||||
|
||||
>>> hasattr(_zip(l2), 'pop')
|
||||
True
|
||||
|
||||
>>> l2 == [(0, 1)]
|
||||
True
|
||||
|
||||
# range/xrange() tests.
|
||||
>>> l1 = _range(3)
|
||||
|
||||
>>> isinstance(l1, list)
|
||||
True
|
||||
|
||||
>>> hasattr(l1, 'pop')
|
||||
True
|
||||
|
||||
>>> l1 == [0, 1, 2]
|
||||
True
|
||||
|
||||
>>> it = _iter_range(3)
|
||||
|
||||
>>> isinstance(it, list)
|
||||
False
|
||||
|
||||
>>> hasattr(it, '__iter__')
|
||||
True
|
||||
|
||||
>>> it == [0, 1, 2]
|
||||
False
|
||||
|
||||
>>> list(it) == [0, 1, 2]
|
||||
True
|
||||
|
||||
# callable() and function meta-data tests.
|
||||
>>> i = 1
|
||||
>>> def f1():
|
||||
... """docstring"""
|
||||
... pass
|
||||
|
||||
>>> f2 = lambda x: x
|
||||
|
||||
>>> _callable(i)
|
||||
False
|
||||
|
||||
>>> _callable(f1)
|
||||
True
|
||||
|
||||
>>> _callable(f2)
|
||||
True
|
||||
|
||||
>>> _func_name(f1) == 'f1'
|
||||
True
|
||||
|
||||
>>> _func_doc(f1) == 'docstring'
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,51 @@
|
||||
=Publish / Subscribe DP Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
Basic Publisher and Subscriber object tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.core import Publisher, Subscriber, PrettyPrinter
|
||||
|
||||
>>> class Subject(Publisher):
|
||||
... pass
|
||||
|
||||
|
||||
>>> class Observer(Subscriber):
|
||||
... def __init__(self, id):
|
||||
... self.id = id
|
||||
...
|
||||
... def update(self, data):
|
||||
... print repr(self), data
|
||||
...
|
||||
... def __repr__(self):
|
||||
... return '%s(%r)' % (self.__class__.__name__, self.id)
|
||||
...
|
||||
|
||||
>>> s = Subject()
|
||||
|
||||
>>> s.attach(Observer('foo'))
|
||||
>>> s.attach(Observer('bar'))
|
||||
|
||||
#FIXME: >>> pp = PrettyPrinter()
|
||||
#FIXME: >>> s.attach(pp)
|
||||
|
||||
>>> data = {'foo': 42, 'list': [1,'2', list(range(10))], 'strings': ['foo', 'bar', 'baz', 'quux']}
|
||||
>>> s.notify(data)
|
||||
Observer('foo') {'foo': 42, 'list': [1, '2', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]], 'strings': ['foo', 'bar', 'baz', 'quux']}
|
||||
Observer('bar') {'foo': 42, 'list': [1, '2', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]], 'strings': ['foo', 'bar', 'baz', 'quux']}
|
||||
|
||||
#FIXME: >>> s.detach(pp)
|
||||
>>> s.notify(['foo', 'bar', 'baz'])
|
||||
Observer('foo') ['foo', 'bar', 'baz']
|
||||
Observer('bar') ['foo', 'bar', 'baz']
|
||||
|
||||
>>> s.attach('foo')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: 'foo' does not support required interface!
|
||||
|
||||
>>> s.detach('foo')
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,205 @@
|
||||
=IEEE EUI-64 Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
IEEE EUI-64 tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> eui = EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
>>> eui
|
||||
EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
|
||||
>>> eui.oui
|
||||
OUI('00-1B-77')
|
||||
|
||||
>>> eui.ei
|
||||
'FF-FE-49-54-FD'
|
||||
|
||||
>>> eui.eui64()
|
||||
EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
|
||||
>>> mac = EUI('00-0F-1F-12-E7-33')
|
||||
>>> ip = mac.ipv6_link_local()
|
||||
>>> ip
|
||||
IPAddress('fe80::20f:1fff:fe12:e733')
|
||||
|
||||
>>> mac.eui64()
|
||||
EUI('00-0F-1F-FF-FE-12-E7-33')
|
||||
|
||||
}}}
|
||||
|
||||
Individual Address Block tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> lower_eui = EUI('00-50-C2-05-C0-00')
|
||||
>>> upper_eui = EUI('00-50-C2-05-CF-FF')
|
||||
|
||||
>>> lower_eui.is_iab()
|
||||
True
|
||||
|
||||
>>> str(lower_eui.oui)
|
||||
'00-50-C2'
|
||||
|
||||
>>> str(lower_eui.iab)
|
||||
'00-50-C2-05-C0-00'
|
||||
|
||||
>>> lower_eui.ei
|
||||
'05-C0-00'
|
||||
|
||||
>>> int(lower_eui.oui) == 0x0050c2
|
||||
True
|
||||
|
||||
>>> int(lower_eui.iab) == 0x0050c205c
|
||||
True
|
||||
|
||||
>>> upper_eui.is_iab()
|
||||
True
|
||||
|
||||
>>> str(upper_eui.oui)
|
||||
'00-50-C2'
|
||||
|
||||
>>> str(upper_eui.iab)
|
||||
'00-50-C2-05-C0-00'
|
||||
|
||||
>>> upper_eui.ei
|
||||
'05-CF-FF'
|
||||
|
||||
>>> int(upper_eui.oui) == 0x0050c2
|
||||
True
|
||||
|
||||
>>> int(upper_eui.iab) == 0x0050c205c
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Constructor tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> eui = EUI('00-90-96-AF-CC-39')
|
||||
|
||||
>>> eui == EUI('0-90-96-AF-CC-39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('00-90-96-af-cc-39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('00:90:96:AF:CC:39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('00:90:96:af:cc:39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('0090-96AF-CC39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('0090:96af:cc39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('009096-AFCC39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('009096:AFCC39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('009096AFCC39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('009096afcc39')
|
||||
True
|
||||
|
||||
>>> EUI('01-00-00-00-00-00') == EUI('010000000000')
|
||||
True
|
||||
|
||||
>>> EUI('01-00-00-00-00-00') == EUI('10000000000')
|
||||
True
|
||||
|
||||
>>> EUI('01-00-00-01-00-00') == EUI('010000:010000')
|
||||
True
|
||||
|
||||
>>> EUI('01-00-00-01-00-00') == EUI('10000:10000')
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
EUI-48 and EUI-64 indentifiers of the same value are *not* equivalent.
|
||||
|
||||
{{{
|
||||
|
||||
>>> eui48 = EUI('01-00-00-01-00-00')
|
||||
>>> int(eui48) == 1099511693312
|
||||
True
|
||||
|
||||
>>> eui64 = EUI('00-00-01-00-00-01-00-00')
|
||||
>>> int(eui64) == 1099511693312
|
||||
True
|
||||
|
||||
>>> eui48 == eui64
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Sortability
|
||||
|
||||
{{{
|
||||
|
||||
>>> import random
|
||||
|
||||
>>> eui_list = [EUI(0, 64), EUI(0), EUI(0xffffffffffff, dialect=mac_unix), EUI(0x1000000000000)]
|
||||
|
||||
>>> random.shuffle(eui_list)
|
||||
|
||||
>>> eui_list.sort()
|
||||
|
||||
>>> for eui in eui_list:
|
||||
... str(eui), eui.version
|
||||
('00-00-00-00-00-00', 48)
|
||||
('ff:ff:ff:ff:ff:ff', 48)
|
||||
('00-00-00-00-00-00-00-00', 64)
|
||||
('00-01-00-00-00-00-00-00', 64)
|
||||
|
||||
}}}
|
||||
|
||||
Persistence
|
||||
|
||||
{{{
|
||||
|
||||
>>> import pickle
|
||||
|
||||
>>> eui1 = EUI('00-00-00-01-02-03')
|
||||
>>> eui2 = pickle.loads(pickle.dumps(eui1))
|
||||
>>> eui1 == eui2
|
||||
True
|
||||
|
||||
>>> eui1 = EUI('00-00-00-01-02-03', dialect=mac_cisco)
|
||||
>>> eui2 = pickle.loads(pickle.dumps(eui1))
|
||||
>>> eui1 == eui2
|
||||
True
|
||||
|
||||
>>> eui1.dialect == eui2.dialect
|
||||
True
|
||||
|
||||
>>> oui1 = EUI('00-00-00-01-02-03').oui
|
||||
>>> oui2 = pickle.loads(pickle.dumps(oui1))
|
||||
>>> oui1 == oui2
|
||||
True
|
||||
>>> oui1.records == oui2.records
|
||||
True
|
||||
|
||||
>>> iab1 = EUI('00-50-C2-00-1F-FF').iab
|
||||
>>> iab2 = pickle.loads(pickle.dumps(iab1))
|
||||
>>> iab1 == iab2
|
||||
True
|
||||
>>> iab1.record == iab2.record
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
=IEEE EUI-64 Identifier Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Basic operations.
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
>>> mac
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> eui = mac.eui64()
|
||||
>>> eui
|
||||
EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
|
||||
>>> int(eui) == 7731765737772285
|
||||
True
|
||||
|
||||
>>> eui.packed
|
||||
'\x00\x1bw\xff\xfeIT\xfd'
|
||||
|
||||
>>> eui.bin
|
||||
'0b11011011101111111111111111110010010010101010011111101'
|
||||
|
||||
>>> eui.bits()
|
||||
'00000000-00011011-01110111-11111111-11111110-01001001-01010100-11111101'
|
||||
|
||||
}}}
|
||||
|
||||
IPv6 interoperability
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> eui = mac.eui64()
|
||||
|
||||
>>> mac
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> eui
|
||||
EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
|
||||
>>> mac.ipv6_link_local()
|
||||
IPAddress('fe80::21b:77ff:fe49:54fd')
|
||||
|
||||
>>> eui.ipv6_link_local()
|
||||
IPAddress('fe80::21b:77ff:fe49:54fd')
|
||||
@@ -0,0 +1,52 @@
|
||||
=IEEE Publish/Subscribe Parser Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
Basic OUIIndexParser and FileIndexer object tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.eui.ieee import OUIIndexParser, IABIndexParser, FileIndexer
|
||||
>>> from cStringIO import StringIO
|
||||
|
||||
>>> infile = StringIO()
|
||||
>>> outfile = StringIO()
|
||||
>>> infile.write("""
|
||||
... 00-CA-FE (hex) ACME CORPORATION
|
||||
... 00CAFE (base 16) ACME CORPORATION
|
||||
... 1 MAIN STREET
|
||||
... SPRINGFIELD
|
||||
... UNITED STATES
|
||||
... """)
|
||||
|
||||
>>> infile.seek(0)
|
||||
>>> iab_parser = OUIIndexParser(infile)
|
||||
>>> iab_parser.attach(FileIndexer(outfile))
|
||||
>>> iab_parser.parse()
|
||||
>>> print outfile.getvalue(),
|
||||
51966,1,210
|
||||
|
||||
}}}
|
||||
|
||||
Basic IABIndexParser and FileIndexer object tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> infile = StringIO()
|
||||
>>> outfile = StringIO()
|
||||
>>> infile.write("""
|
||||
... 00-50-C2 (hex) ACME CORPORATION
|
||||
... ABC000-ABCFFF (base 16) ACME CORPORATION
|
||||
... 1 MAIN STREET
|
||||
... SPRINGFIELD
|
||||
... UNITED STATES
|
||||
... """)
|
||||
|
||||
>>> infile.seek(0)
|
||||
>>> iab_parser = IABIndexParser(infile)
|
||||
>>> iab_parser.attach(FileIndexer(outfile))
|
||||
>>> iab_parser.parse()
|
||||
>>> print outfile.getvalue(),
|
||||
84683452,1,181
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,188 @@
|
||||
First of all you need to pull the various MAC related classes and functions into your namespace.
|
||||
|
||||
.. note:: Do this for the purpose of this tutorial only. In your own code, you should be explicit about the classes, functions and constants you import to avoid name clashes.
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
You can reasonably safely import everything from the netaddr namespace as care has been taken to only export the necessary classes, functions and constants.
|
||||
|
||||
Always hand pick your imports if you are unsure about possible name clashes.
|
||||
|
||||
----------------
|
||||
Basic operations
|
||||
----------------
|
||||
|
||||
Instances of the EUI class are used to represent MAC addresses.
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
|
||||
Standard repr() access returns a Python statement that can reconstruct the MAC address object from scratch if executed in the Python interpreter.
|
||||
|
||||
>>> mac
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
Accessing the EUI object in the string context.
|
||||
|
||||
>>> str(mac)
|
||||
'00-1B-77-49-54-FD'
|
||||
>>> '%s' % mac
|
||||
'00-1B-77-49-54-FD'
|
||||
|
||||
Here are a few other common properties.
|
||||
|
||||
>>> str(mac), str(mac.oui), mac.ei, mac.version
|
||||
('00-1B-77-49-54-FD', '00-1B-77', '49-54-FD', 48)
|
||||
|
||||
-------------------------
|
||||
Numerical representations
|
||||
-------------------------
|
||||
|
||||
You can view an individual IP address in various other formats.
|
||||
|
||||
>>> int(mac) == 117965411581
|
||||
True
|
||||
>>> hex(mac)
|
||||
'0x1b774954fd'
|
||||
>>> oct(mac)
|
||||
'01556722252375'
|
||||
>>> mac.bits()
|
||||
'00000000-00011011-01110111-01001001-01010100-11111101'
|
||||
>>> mac.bin
|
||||
'0b1101101110111010010010101010011111101'
|
||||
|
||||
----------
|
||||
Formatting
|
||||
----------
|
||||
|
||||
It is very common to see MAC address in many different formats other than the standard IEEE EUI-48.
|
||||
|
||||
The EUI class constructor handles all these common forms.
|
||||
|
||||
>>> EUI('00-1B-77-49-54-FD')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
IEEE EUI-48 lowercase format
|
||||
|
||||
>>> EUI('00-1b-77-49-54-fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
Common UNIX format
|
||||
|
||||
>>> EUI('0:1b:77:49:54:fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
Cisco triple hextet format
|
||||
|
||||
>>> EUI('001b:7749:54fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
>>> EUI('1b:7749:54fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
>>> EUI('1B:7749:54FD')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
Bare MAC addresses (no delimiters)
|
||||
|
||||
>>> EUI('001b774954fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
>>> EUI('01B774954FD')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
PostreSQL format (found in documentation)
|
||||
|
||||
>>> EUI('001B77:4954FD')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
It is equally possible to specify a selected format for your MAC string output in the form of a 'dialect' class. It's use is similar to the dialect class used in the Python standard library csv module.
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
>>> mac
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
>>> mac.dialect = mac_unix
|
||||
>>> mac
|
||||
EUI('0:1b:77:49:54:fd')
|
||||
>>> mac.dialect = mac_cisco
|
||||
>>> mac
|
||||
EUI('001b.7749.54fd')
|
||||
>>> mac.dialect = mac_bare
|
||||
>>> mac
|
||||
EUI('001B774954FD')
|
||||
>>> mac.dialect = mac_pgsql
|
||||
>>> mac
|
||||
EUI('001b77:4954fd')
|
||||
|
||||
You can of course, create your own dialect classes to customise the MAC formatting if the standard ones do not suit your needs.
|
||||
|
||||
Here's a tweaked UNIX MAC dialect that generates uppercase, zero-filled octets.
|
||||
|
||||
>>> class mac_custom(mac_unix): pass
|
||||
>>> mac_custom.word_fmt = '%.2X'
|
||||
>>> mac = EUI('00-1B-77-49-54-FD', dialect=mac_custom)
|
||||
>>> mac
|
||||
EUI('00:1B:77:49:54:FD')
|
||||
|
||||
-----------------------------------
|
||||
Querying organisational information
|
||||
-----------------------------------
|
||||
|
||||
EUI objects provide an interface to the OUI (Organisationally Unique Identifier) and IAB (Individual Address Block) registration databases available from the IEEE.
|
||||
|
||||
Here is how you query an OUI with the EUI interface.
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
>>> oui = mac.oui
|
||||
>>> oui
|
||||
OUI('00-1B-77')
|
||||
>>> oui.registration().address
|
||||
['Lot 8, Jalan Hi-Tech 2/3', 'Kulim Hi-Tech Park', 'Kulim Kedah 09000', 'MALAYSIA']
|
||||
>>> oui.registration().org
|
||||
'Intel Corporate'
|
||||
|
||||
You can also use OUI objects directly without going through the EUI interface.
|
||||
|
||||
A few OUI records have multiple registrations against them. I'm not sure if this is recording historical information or just a quirk of the IEEE reigstration process.
|
||||
|
||||
This example show you how you access them individually by specifying an index number.
|
||||
|
||||
>>> oui = OUI(524336) # OUI constructor accepts integer values too.
|
||||
>>> oui
|
||||
OUI('08-00-30')
|
||||
>>> oui.registration(0).address
|
||||
['2380 N. ROSE AVENUE', 'OXNARD CA 93010', 'UNITED STATES']
|
||||
>>> oui.registration(0).org
|
||||
'NETWORK RESEARCH CORPORATION'
|
||||
>>> oui.registration(0).oui
|
||||
'08-00-30'
|
||||
>>> oui.registration(1).address
|
||||
['CH-1211 GENEVE 23', 'SUISSE/SWITZ', 'SWITZERLAND']
|
||||
>>> oui.registration(1).org
|
||||
'CERN'
|
||||
>>> oui.registration(1).oui
|
||||
'08-00-30'
|
||||
>>> oui.registration(2).address
|
||||
['GPO BOX 2476V', 'MELBOURNE VIC 3001', 'AUSTRALIA']
|
||||
>>> oui.registration(2).org
|
||||
'ROYAL MELBOURNE INST OF TECH'
|
||||
>>> oui.registration(2).oui
|
||||
'08-00-30'
|
||||
>>> for i in range(oui.reg_count):
|
||||
... str(oui), oui.registration(i).org
|
||||
...
|
||||
('08-00-30', 'NETWORK RESEARCH CORPORATION')
|
||||
('08-00-30', 'CERN')
|
||||
('08-00-30', 'ROYAL MELBOURNE INST OF TECH')
|
||||
|
||||
Here is how you query an IAB with the EUI interface.
|
||||
|
||||
>>> mac = EUI('00-50-C2-00-0F-01')
|
||||
>>> mac.is_iab()
|
||||
True
|
||||
>>> iab = mac.iab
|
||||
>>> iab
|
||||
IAB('00-50-C2-00-00-00')
|
||||
>>> iab.registration()
|
||||
{'address': ['2101 Superior Avenue', 'Cleveland OH 44114', 'UNITED STATES'],
|
||||
'iab': '00-50-C2-00-00-00',
|
||||
...
|
||||
'offset': 68,
|
||||
'org': 'T.L.S. Corp.',
|
||||
'size': 133}
|
||||
@@ -0,0 +1,202 @@
|
||||
=Abbreviated CIDR Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Abbreviation tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ranges = (
|
||||
... (IPAddress('::'), IPAddress('::')),
|
||||
... (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')),
|
||||
... (IPAddress('::'), IPAddress('::255.255.255.255')),
|
||||
... (IPAddress('0.0.0.0'), IPAddress('0.0.0.0')),
|
||||
... )
|
||||
|
||||
>>> sorted(ranges)
|
||||
[(IPAddress('0.0.0.0'), IPAddress('0.0.0.0')), (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')), (IPAddress('::'), IPAddress('::')), (IPAddress('::'), IPAddress('::255.255.255.255'))]
|
||||
|
||||
# Integer values.
|
||||
>>> cidr_abbrev_to_verbose(-1)
|
||||
-1
|
||||
|
||||
# Class A
|
||||
>>> cidr_abbrev_to_verbose(0)
|
||||
'0.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose(10)
|
||||
'10.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose(127)
|
||||
'127.0.0.0/8'
|
||||
|
||||
# Class B
|
||||
>>> cidr_abbrev_to_verbose(128)
|
||||
'128.0.0.0/16'
|
||||
>>> cidr_abbrev_to_verbose(191)
|
||||
'191.0.0.0/16'
|
||||
|
||||
# Class C
|
||||
>>> cidr_abbrev_to_verbose(192)
|
||||
'192.0.0.0/24'
|
||||
>>> cidr_abbrev_to_verbose(223)
|
||||
'223.0.0.0/24'
|
||||
|
||||
# Class D (multicast)
|
||||
>>> cidr_abbrev_to_verbose(224)
|
||||
'224.0.0.0/4'
|
||||
>>> cidr_abbrev_to_verbose(225)
|
||||
'225.0.0.0/4'
|
||||
>>> cidr_abbrev_to_verbose(239)
|
||||
'239.0.0.0/4'
|
||||
|
||||
# Class E (reserved)
|
||||
>>> cidr_abbrev_to_verbose(240)
|
||||
'240.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose(254)
|
||||
'254.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose(255)
|
||||
'255.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose(256)
|
||||
256
|
||||
|
||||
# String values.
|
||||
>>> cidr_abbrev_to_verbose('-1')
|
||||
'-1'
|
||||
|
||||
# Class A
|
||||
>>> cidr_abbrev_to_verbose('0')
|
||||
'0.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('10')
|
||||
'10.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('127')
|
||||
'127.0.0.0/8'
|
||||
|
||||
# Class B
|
||||
>>> cidr_abbrev_to_verbose('128')
|
||||
'128.0.0.0/16'
|
||||
>>> cidr_abbrev_to_verbose('191')
|
||||
'191.0.0.0/16'
|
||||
|
||||
# Class C
|
||||
>>> cidr_abbrev_to_verbose('192')
|
||||
'192.0.0.0/24'
|
||||
>>> cidr_abbrev_to_verbose('223')
|
||||
'223.0.0.0/24'
|
||||
|
||||
# Class D (multicast)
|
||||
>>> cidr_abbrev_to_verbose('224')
|
||||
'224.0.0.0/4'
|
||||
>>> cidr_abbrev_to_verbose('225')
|
||||
'225.0.0.0/4'
|
||||
>>> cidr_abbrev_to_verbose('239')
|
||||
'239.0.0.0/4'
|
||||
|
||||
# Class E (reserved)
|
||||
>>> cidr_abbrev_to_verbose('240')
|
||||
'240.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose('254')
|
||||
'254.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose('255')
|
||||
'255.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose('256')
|
||||
'256'
|
||||
|
||||
>>> cidr_abbrev_to_verbose('128/8')
|
||||
'128.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('128.0/8')
|
||||
'128.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('128.0.0.0/8')
|
||||
'128.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('128.0.0/8')
|
||||
'128.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('192.168')
|
||||
'192.168.0.0/24'
|
||||
>>> cidr_abbrev_to_verbose('192.0.2')
|
||||
'192.0.2.0/24'
|
||||
>>> cidr_abbrev_to_verbose('192.0.2.0')
|
||||
'192.0.2.0/24'
|
||||
>>> cidr_abbrev_to_verbose('0.0.0.0')
|
||||
'0.0.0.0/8'
|
||||
|
||||
# No IPv6 support current.
|
||||
>>> cidr_abbrev_to_verbose('::/128')
|
||||
'::/128'
|
||||
|
||||
# IPv6 proper, not IPv4 mapped?
|
||||
>>> cidr_abbrev_to_verbose('::10/128')
|
||||
'::10/128'
|
||||
>>> cidr_abbrev_to_verbose('0.0.0.0.0')
|
||||
'0.0.0.0.0'
|
||||
>>> cidr_abbrev_to_verbose('')
|
||||
''
|
||||
>>> cidr_abbrev_to_verbose(None)
|
||||
|
||||
>>> cidr_abbrev_to_verbose([])
|
||||
[]
|
||||
>>> cidr_abbrev_to_verbose({})
|
||||
{}
|
||||
|
||||
}}}
|
||||
|
||||
Negative testing.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_abbrev_to_verbose('192.0.2.0')
|
||||
'192.0.2.0/24'
|
||||
|
||||
>>> cidr_abbrev_to_verbose('192.0.2.0/32')
|
||||
'192.0.2.0/32'
|
||||
|
||||
#FIXME: >>> cidr_abbrev_to_verbose('192.0.2.0/33')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: prefixlen in address '192.0.2.0/33' out of range for IPv4!
|
||||
|
||||
}}}
|
||||
|
||||
IPv4 octet expansion routine.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy import ipv4
|
||||
|
||||
>>> ipv4.expand_partial_address('10')
|
||||
'10.0.0.0'
|
||||
|
||||
>>> ipv4.expand_partial_address('10.1')
|
||||
'10.1.0.0'
|
||||
|
||||
>>> ipv4.expand_partial_address('192.168.1')
|
||||
'192.168.1.0'
|
||||
|
||||
}}}
|
||||
|
||||
IPNetwork constructor testing.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.168/16')
|
||||
IPNetwork('192.168.0.0/16')
|
||||
|
||||
>>> IPNetwork('192.168.0.15')
|
||||
IPNetwork('192.168.0.15/32')
|
||||
|
||||
>>> IPNetwork('192.168')
|
||||
IPNetwork('192.168.0.0/32')
|
||||
|
||||
>>> IPNetwork('192.168', implicit_prefix=True)
|
||||
IPNetwork('192.168.0.0/24')
|
||||
|
||||
>>> IPNetwork('192.168', True)
|
||||
IPNetwork('192.168.0.0/24')
|
||||
|
||||
>>> IPNetwork('10.0.0.1', True)
|
||||
IPNetwork('10.0.0.1/8')
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,44 @@
|
||||
=Binary and numerical operations on IP addresses=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
==Addition and Subtraction with integers ==
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.0') + 1
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> 1 + IPAddress('192.0.2.0')
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> IPAddress('192.0.2.1') - 1
|
||||
IPAddress('192.0.2.0')
|
||||
|
||||
>>> 1 - IPAddress('192.0.2.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: result outside valid IP address boundary!
|
||||
|
||||
}}}
|
||||
|
||||
==Binary operations==
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.15') & IPAddress('255.255.255.0')
|
||||
IPAddress('192.0.2.0')
|
||||
|
||||
>>> IPAddress('255.255.0.0') | IPAddress('0.0.255.255')
|
||||
IPAddress('255.255.255.255')
|
||||
|
||||
>>> IPAddress('255.255.0.0') ^ IPAddress('255.0.0.0')
|
||||
IPAddress('0.255.0.0')
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,157 @@
|
||||
=IP Range Boundary Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
>>> import pprint
|
||||
|
||||
}}}
|
||||
|
||||
`iter_iprange()` iterator boundary tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> pprint.pprint(list(iter_iprange('192.0.2.0', '192.0.2.7')))
|
||||
[IPAddress('192.0.2.0'),
|
||||
IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.6'),
|
||||
IPAddress('192.0.2.7')]
|
||||
|
||||
>>> pprint.pprint(list(iter_iprange('::ffff:192.0.2.0', '::ffff:192.0.2.7')))
|
||||
[IPAddress('::ffff:192.0.2.0'),
|
||||
IPAddress('::ffff:192.0.2.1'),
|
||||
IPAddress('::ffff:192.0.2.2'),
|
||||
IPAddress('::ffff:192.0.2.3'),
|
||||
IPAddress('::ffff:192.0.2.4'),
|
||||
IPAddress('::ffff:192.0.2.5'),
|
||||
IPAddress('::ffff:192.0.2.6'),
|
||||
IPAddress('::ffff:192.0.2.7')]
|
||||
|
||||
|
||||
}}}
|
||||
|
||||
`IPNetwork()` iterator boundary tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> pprint.pprint(list(IPNetwork('192.0.2.0/29')[0:-1]))
|
||||
[IPAddress('192.0.2.0'),
|
||||
IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.6')]
|
||||
|
||||
>>> pprint.pprint(list(IPNetwork('192.0.2.0/29')[::-1]))
|
||||
[IPAddress('192.0.2.7'),
|
||||
IPAddress('192.0.2.6'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.0')]
|
||||
|
||||
>>> pprint.pprint(list(IPNetwork('192.0.2.0/29').iter_hosts()))
|
||||
[IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.6')]
|
||||
|
||||
>>> pprint.pprint(list(IPNetwork('::ffff:192.0.2.0/125').iter_hosts()))
|
||||
[IPAddress('::ffff:192.0.2.0'),
|
||||
IPAddress('::ffff:192.0.2.1'),
|
||||
IPAddress('::ffff:192.0.2.2'),
|
||||
IPAddress('::ffff:192.0.2.3'),
|
||||
IPAddress('::ffff:192.0.2.4'),
|
||||
IPAddress('::ffff:192.0.2.5'),
|
||||
IPAddress('::ffff:192.0.2.6'),
|
||||
IPAddress('::ffff:192.0.2.7')]
|
||||
|
||||
}}}
|
||||
|
||||
`IPRange()` iterator boundary tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> pprint.pprint(list(IPRange('192.0.2.0', '192.0.2.7')))
|
||||
[IPAddress('192.0.2.0'),
|
||||
IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.6'),
|
||||
IPAddress('192.0.2.7')]
|
||||
|
||||
>>> pprint.pprint(list(IPRange('::ffff:192.0.2.0', '::ffff:192.0.2.7')))
|
||||
[IPAddress('::ffff:192.0.2.0'),
|
||||
IPAddress('::ffff:192.0.2.1'),
|
||||
IPAddress('::ffff:192.0.2.2'),
|
||||
IPAddress('::ffff:192.0.2.3'),
|
||||
IPAddress('::ffff:192.0.2.4'),
|
||||
IPAddress('::ffff:192.0.2.5'),
|
||||
IPAddress('::ffff:192.0.2.6'),
|
||||
IPAddress('::ffff:192.0.2.7')]
|
||||
|
||||
}}}
|
||||
|
||||
Boolean contexts.
|
||||
|
||||
{{{
|
||||
|
||||
>>> bool(IPAddress('0.0.0.0'))
|
||||
False
|
||||
|
||||
>>> bool(IPAddress('0.0.0.1'))
|
||||
True
|
||||
|
||||
>>> bool(IPAddress('255.255.255.255'))
|
||||
True
|
||||
|
||||
>>> bool(IPNetwork('0.0.0.0/0'))
|
||||
True
|
||||
|
||||
>>> bool(IPNetwork('::/0'))
|
||||
True
|
||||
|
||||
>>> bool(IPRange('0.0.0.0', '255.255.255.255'))
|
||||
True
|
||||
|
||||
>>> bool(IPRange('0.0.0.0', '0.0.0.0'))
|
||||
True
|
||||
|
||||
>>> bool(IPGlob('*.*.*.*'))
|
||||
True
|
||||
|
||||
>>> bool(IPGlob('0.0.0.0'))
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
`IPAddress()` negative increment tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress('0.0.0.0')
|
||||
>>> ip += -1
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: result outside valid IP address boundary!
|
||||
|
||||
>>> ip = IPAddress('255.255.255.255')
|
||||
>>> ip -= -1
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: result outside valid IP address boundary!
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,449 @@
|
||||
=CIDR Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic IP Range Tuple Sorting==
|
||||
|
||||
{{{
|
||||
|
||||
>>> ranges = (
|
||||
... (IPAddress('::'), IPAddress('::')),
|
||||
... (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')),
|
||||
... (IPAddress('::'), IPAddress('::255.255.255.255')),
|
||||
... (IPAddress('0.0.0.0'), IPAddress('0.0.0.0')),
|
||||
... )
|
||||
|
||||
>>> sorted(ranges)
|
||||
[(IPAddress('0.0.0.0'), IPAddress('0.0.0.0')), (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')), (IPAddress('::'), IPAddress('::')), (IPAddress('::'), IPAddress('::255.255.255.255'))]
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 range to CIDR conversion.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('0.0.0.1', '255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
IPNetwork('0.0.0.1/32')
|
||||
IPNetwork('0.0.0.2/31')
|
||||
IPNetwork('0.0.0.4/30')
|
||||
IPNetwork('0.0.0.8/29')
|
||||
IPNetwork('0.0.0.16/28')
|
||||
IPNetwork('0.0.0.32/27')
|
||||
IPNetwork('0.0.0.64/26')
|
||||
IPNetwork('0.0.0.128/25')
|
||||
IPNetwork('0.0.1.0/24')
|
||||
IPNetwork('0.0.2.0/23')
|
||||
IPNetwork('0.0.4.0/22')
|
||||
IPNetwork('0.0.8.0/21')
|
||||
IPNetwork('0.0.16.0/20')
|
||||
IPNetwork('0.0.32.0/19')
|
||||
IPNetwork('0.0.64.0/18')
|
||||
IPNetwork('0.0.128.0/17')
|
||||
IPNetwork('0.1.0.0/16')
|
||||
IPNetwork('0.2.0.0/15')
|
||||
IPNetwork('0.4.0.0/14')
|
||||
IPNetwork('0.8.0.0/13')
|
||||
IPNetwork('0.16.0.0/12')
|
||||
IPNetwork('0.32.0.0/11')
|
||||
IPNetwork('0.64.0.0/10')
|
||||
IPNetwork('0.128.0.0/9')
|
||||
IPNetwork('1.0.0.0/8')
|
||||
IPNetwork('2.0.0.0/7')
|
||||
IPNetwork('4.0.0.0/6')
|
||||
IPNetwork('8.0.0.0/5')
|
||||
IPNetwork('16.0.0.0/4')
|
||||
IPNetwork('32.0.0.0/3')
|
||||
IPNetwork('64.0.0.0/2')
|
||||
IPNetwork('128.0.0.0/2')
|
||||
IPNetwork('192.0.0.0/3')
|
||||
IPNetwork('224.0.0.0/4')
|
||||
IPNetwork('240.0.0.0/5')
|
||||
IPNetwork('248.0.0.0/6')
|
||||
IPNetwork('252.0.0.0/7')
|
||||
IPNetwork('254.0.0.0/8')
|
||||
IPNetwork('255.0.0.0/9')
|
||||
IPNetwork('255.128.0.0/10')
|
||||
IPNetwork('255.192.0.0/11')
|
||||
IPNetwork('255.224.0.0/12')
|
||||
IPNetwork('255.240.0.0/13')
|
||||
IPNetwork('255.248.0.0/14')
|
||||
IPNetwork('255.252.0.0/15')
|
||||
IPNetwork('255.254.0.0/16')
|
||||
IPNetwork('255.255.0.0/17')
|
||||
IPNetwork('255.255.128.0/18')
|
||||
IPNetwork('255.255.192.0/19')
|
||||
IPNetwork('255.255.224.0/20')
|
||||
IPNetwork('255.255.240.0/21')
|
||||
IPNetwork('255.255.248.0/22')
|
||||
IPNetwork('255.255.252.0/23')
|
||||
IPNetwork('255.255.254.0/24')
|
||||
IPNetwork('255.255.255.0/25')
|
||||
IPNetwork('255.255.255.128/26')
|
||||
IPNetwork('255.255.255.192/27')
|
||||
IPNetwork('255.255.255.224/28')
|
||||
IPNetwork('255.255.255.240/29')
|
||||
IPNetwork('255.255.255.248/30')
|
||||
IPNetwork('255.255.255.252/31')
|
||||
IPNetwork('255.255.255.254/32')
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 mapped IPv6 range to CIDR.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('::ffff:1', '::ffff:255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
IPNetwork('::255.255.0.1/128')
|
||||
IPNetwork('::255.255.0.2/127')
|
||||
IPNetwork('::255.255.0.4/126')
|
||||
IPNetwork('::255.255.0.8/125')
|
||||
IPNetwork('::255.255.0.16/124')
|
||||
IPNetwork('::255.255.0.32/123')
|
||||
IPNetwork('::255.255.0.64/122')
|
||||
IPNetwork('::255.255.0.128/121')
|
||||
IPNetwork('::255.255.1.0/120')
|
||||
IPNetwork('::255.255.2.0/119')
|
||||
IPNetwork('::255.255.4.0/118')
|
||||
IPNetwork('::255.255.8.0/117')
|
||||
IPNetwork('::255.255.16.0/116')
|
||||
IPNetwork('::255.255.32.0/115')
|
||||
IPNetwork('::255.255.64.0/114')
|
||||
IPNetwork('::255.255.128.0/113')
|
||||
IPNetwork('::1:0:0/96')
|
||||
IPNetwork('::2:0:0/95')
|
||||
IPNetwork('::4:0:0/94')
|
||||
IPNetwork('::8:0:0/93')
|
||||
IPNetwork('::10:0:0/92')
|
||||
IPNetwork('::20:0:0/91')
|
||||
IPNetwork('::40:0:0/90')
|
||||
IPNetwork('::80:0:0/89')
|
||||
IPNetwork('::100:0:0/88')
|
||||
IPNetwork('::200:0:0/87')
|
||||
IPNetwork('::400:0:0/86')
|
||||
IPNetwork('::800:0:0/85')
|
||||
IPNetwork('::1000:0:0/84')
|
||||
IPNetwork('::2000:0:0/83')
|
||||
IPNetwork('::4000:0:0/82')
|
||||
IPNetwork('::8000:0:0/82')
|
||||
IPNetwork('::c000:0:0/83')
|
||||
IPNetwork('::e000:0:0/84')
|
||||
IPNetwork('::f000:0:0/85')
|
||||
IPNetwork('::f800:0:0/86')
|
||||
IPNetwork('::fc00:0:0/87')
|
||||
IPNetwork('::fe00:0:0/88')
|
||||
IPNetwork('::ff00:0:0/89')
|
||||
IPNetwork('::ff80:0:0/90')
|
||||
IPNetwork('::ffc0:0:0/91')
|
||||
IPNetwork('::ffe0:0:0/92')
|
||||
IPNetwork('::fff0:0:0/93')
|
||||
IPNetwork('::fff8:0:0/94')
|
||||
IPNetwork('::fffc:0:0/95')
|
||||
IPNetwork('::fffe:0:0/96')
|
||||
IPNetwork('::ffff:0.0.0.0/97')
|
||||
IPNetwork('::ffff:128.0.0.0/98')
|
||||
IPNetwork('::ffff:192.0.0.0/99')
|
||||
IPNetwork('::ffff:224.0.0.0/100')
|
||||
IPNetwork('::ffff:240.0.0.0/101')
|
||||
IPNetwork('::ffff:248.0.0.0/102')
|
||||
IPNetwork('::ffff:252.0.0.0/103')
|
||||
IPNetwork('::ffff:254.0.0.0/104')
|
||||
IPNetwork('::ffff:255.0.0.0/105')
|
||||
IPNetwork('::ffff:255.128.0.0/106')
|
||||
IPNetwork('::ffff:255.192.0.0/107')
|
||||
IPNetwork('::ffff:255.224.0.0/108')
|
||||
IPNetwork('::ffff:255.240.0.0/109')
|
||||
IPNetwork('::ffff:255.248.0.0/110')
|
||||
IPNetwork('::ffff:255.252.0.0/111')
|
||||
IPNetwork('::ffff:255.254.0.0/112')
|
||||
IPNetwork('::ffff:255.255.0.0/113')
|
||||
IPNetwork('::ffff:255.255.128.0/114')
|
||||
IPNetwork('::ffff:255.255.192.0/115')
|
||||
IPNetwork('::ffff:255.255.224.0/116')
|
||||
IPNetwork('::ffff:255.255.240.0/117')
|
||||
IPNetwork('::ffff:255.255.248.0/118')
|
||||
IPNetwork('::ffff:255.255.252.0/119')
|
||||
IPNetwork('::ffff:255.255.254.0/120')
|
||||
IPNetwork('::ffff:255.255.255.0/121')
|
||||
IPNetwork('::ffff:255.255.255.128/122')
|
||||
IPNetwork('::ffff:255.255.255.192/123')
|
||||
IPNetwork('::ffff:255.255.255.224/124')
|
||||
IPNetwork('::ffff:255.255.255.240/125')
|
||||
IPNetwork('::ffff:255.255.255.248/126')
|
||||
IPNetwork('::ffff:255.255.255.252/127')
|
||||
IPNetwork('::ffff:255.255.255.254/128')
|
||||
|
||||
}}}
|
||||
|
||||
RFC 4291 CIDR tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str(IPNetwork('2001:0DB8:0000:CD30:0000:0000:0000:0000/60'))
|
||||
'2001:db8:0:cd30::/60'
|
||||
|
||||
>>> str(IPNetwork('2001:0DB8::CD30:0:0:0:0/60'))
|
||||
'2001:db8:0:cd30::/60'
|
||||
|
||||
>>> str(IPNetwork('2001:0DB8:0:CD30::/60'))
|
||||
'2001:db8:0:cd30::/60'
|
||||
|
||||
}}}
|
||||
|
||||
Equality tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0/255.255.254.0') == IPNetwork('192.0.2.0/23')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.65/255.255.254.0') == IPNetwork('192.0.2.0/23')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.65/255.255.254.0') == IPNetwork('192.0.2.65/23')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.65/255.255.255.0') == IPNetwork('192.0.2.0/23')
|
||||
False
|
||||
|
||||
>>> IPNetwork('192.0.2.65/255.255.254.0') == IPNetwork('192.0.2.65/24')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Slicing tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.0/23')
|
||||
>>> ip.first == 3221225984
|
||||
True
|
||||
|
||||
>>> ip.last == 3221226495
|
||||
True
|
||||
|
||||
>>> ip[0]
|
||||
IPAddress('192.0.2.0')
|
||||
|
||||
>>> ip[-1]
|
||||
IPAddress('192.0.3.255')
|
||||
|
||||
>>> list(ip[::128])
|
||||
[IPAddress('192.0.2.0'), IPAddress('192.0.2.128'), IPAddress('192.0.3.0'), IPAddress('192.0.3.128')]
|
||||
|
||||
>>> ip = IPNetwork('fe80::/10')
|
||||
>>> ip[0]
|
||||
IPAddress('fe80::')
|
||||
|
||||
>>> ip[-1]
|
||||
IPAddress('febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
|
||||
|
||||
>>> ip.size == 332306998946228968225951765070086144
|
||||
True
|
||||
|
||||
>>> list(ip[0:5:1])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: IPv6 slices are not supported!
|
||||
|
||||
|
||||
}}}
|
||||
|
||||
Membership tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.1') in IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.255') in IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') in IPNetwork('192.0.2.0/23')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') in IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPAddress('ffff::1') in IPNetwork('ffff::/127')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/23') in IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Equality tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') == IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') is not IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') != IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') is IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
>>> IPNetwork('fe80::/10') == IPNetwork('fe80::/10')
|
||||
True
|
||||
|
||||
>>> IPNetwork('fe80::/10') is not IPNetwork('fe80::/10')
|
||||
True
|
||||
|
||||
>>> IPNetwork('fe80::/10') != IPNetwork('fe80::/10')
|
||||
False
|
||||
|
||||
>>> IPNetwork('fe80::/10') is IPNetwork('fe80::/10')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Exclusion tests.
|
||||
|
||||
{{{
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1]) - set([1])
|
||||
# set([1])
|
||||
>>> cidr_exclude('192.0.2.1/32', '192.0.2.1/32')
|
||||
[]
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1,2]) - set([2])
|
||||
# set([1])
|
||||
>>> cidr_exclude('192.0.2.0/31', '192.0.2.1/32')
|
||||
[IPNetwork('192.0.2.0/32')]
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1,2,3,4,5,6,7,8]) - set([5,6,7,8])
|
||||
# set([1, 2, 3, 4])
|
||||
>>> cidr_exclude('192.0.2.0/24', '192.0.2.128/25')
|
||||
[IPNetwork('192.0.2.0/25')]
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1,2,3,4,5,6,7,8]) - set([5,6])
|
||||
# set([1, 2, 3, 4, 7, 8])
|
||||
>>> cidr_exclude('192.0.2.0/24', '192.0.2.128/27')
|
||||
[IPNetwork('192.0.2.0/25'), IPNetwork('192.0.2.160/27'), IPNetwork('192.0.2.192/26')]
|
||||
|
||||
# Subtracting a larger range from a smaller one results in an empty
|
||||
# list (rather than a negative CIDR - which would be rather odd)!
|
||||
#
|
||||
# Equivalent to :-
|
||||
# >>> set([1]) - set([1,2,3])
|
||||
# set([])
|
||||
>>> cidr_exclude('192.0.2.1/32', '192.0.2.0/24')
|
||||
[]
|
||||
|
||||
}}}
|
||||
|
||||
Please Note: excluding IP subnets that are not within each other and have no overlaps should return the original target IP object.
|
||||
|
||||
{{{
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1,2,3]) - set([4])
|
||||
# set([1,2,3])
|
||||
>>> cidr_exclude('192.0.2.0/28', '192.0.2.16/32')
|
||||
[IPNetwork('192.0.2.0/28')]
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1]) - set([2,3,4])
|
||||
# set([1])
|
||||
>>> cidr_exclude('192.0.1.255/32', '192.0.2.0/28')
|
||||
[IPNetwork('192.0.1.255/32')]
|
||||
|
||||
}}}
|
||||
|
||||
Merge tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(['192.0.128.0/24', '192.0.129.0/24'])
|
||||
[IPNetwork('192.0.128.0/23')]
|
||||
|
||||
>>> cidr_merge(['192.0.129.0/24', '192.0.130.0/24'])
|
||||
[IPNetwork('192.0.129.0/24'), IPNetwork('192.0.130.0/24')]
|
||||
|
||||
>>> cidr_merge(['192.0.2.112/30', '192.0.2.116/31', '192.0.2.118/31'])
|
||||
[IPNetwork('192.0.2.112/29')]
|
||||
|
||||
>>> cidr_merge(['192.0.2.112/30', '192.0.2.116/32', '192.0.2.118/31'])
|
||||
[IPNetwork('192.0.2.112/30'), IPNetwork('192.0.2.116/32'), IPNetwork('192.0.2.118/31')]
|
||||
|
||||
>>> cidr_merge(['192.0.2.112/31', '192.0.2.116/31', '192.0.2.118/31'])
|
||||
[IPNetwork('192.0.2.112/31'), IPNetwork('192.0.2.116/30')]
|
||||
|
||||
>>> cidr_merge(['192.0.1.254/31',
|
||||
... '192.0.2.0/28',
|
||||
... '192.0.2.16/28',
|
||||
... '192.0.2.32/28',
|
||||
... '192.0.2.48/28',
|
||||
... '192.0.2.64/28',
|
||||
... '192.0.2.80/28',
|
||||
... '192.0.2.96/28',
|
||||
... '192.0.2.112/28',
|
||||
... '192.0.2.128/28',
|
||||
... '192.0.2.144/28',
|
||||
... '192.0.2.160/28',
|
||||
... '192.0.2.176/28',
|
||||
... '192.0.2.192/28',
|
||||
... '192.0.2.208/28',
|
||||
... '192.0.2.224/28',
|
||||
... '192.0.2.240/28',
|
||||
... '192.0.3.0/28'])
|
||||
[IPNetwork('192.0.1.254/31'), IPNetwork('192.0.2.0/24'), IPNetwork('192.0.3.0/28')]
|
||||
|
||||
}}}
|
||||
|
||||
Extended merge tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> import random
|
||||
|
||||
# Start with a single /23 CIDR.
|
||||
|
||||
>>> orig_cidr_ipv4 = IPNetwork('192.0.2.0/23')
|
||||
>>> orig_cidr_ipv6 = IPNetwork('::192.0.2.0/120')
|
||||
|
||||
# Split it into /28 subnet CIDRs (mix CIDR objects and CIDR strings).
|
||||
|
||||
>>> cidr_subnets = []
|
||||
>>> cidr_subnets.extend([str(c) for c in orig_cidr_ipv4.subnet(28)])
|
||||
>>> cidr_subnets.extend(list(orig_cidr_ipv4.subnet(28)))
|
||||
>>> cidr_subnets.extend([str(c) for c in orig_cidr_ipv6.subnet(124)])
|
||||
>>> cidr_subnets.extend(list(orig_cidr_ipv6.subnet(124)))
|
||||
|
||||
# Add a couple of duplicates in to make sure summarization is working OK.
|
||||
|
||||
>>> cidr_subnets.append('192.0.2.1/32')
|
||||
>>> cidr_subnets.append('192.0.2.128/25')
|
||||
>>> cidr_subnets.append('::192.0.2.92/128')
|
||||
|
||||
# Randomize the order of subnets.
|
||||
>>> random.shuffle(cidr_subnets)
|
||||
|
||||
# Perform summarization operation.
|
||||
>>> merged_cidrs = cidr_merge(cidr_subnets)
|
||||
>>> merged_cidrs
|
||||
[IPNetwork('192.0.2.0/23'), IPNetwork('::192.0.2.0/120')]
|
||||
|
||||
>>> merged_cidrs == [orig_cidr_ipv4, orig_cidr_ipv6]
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,216 @@
|
||||
=IP Constructor Stress Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress constructor - integer values.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress(1)
|
||||
IPAddress('0.0.0.1')
|
||||
|
||||
>>> IPAddress(1, 4)
|
||||
IPAddress('0.0.0.1')
|
||||
|
||||
>>> IPAddress(1, 6)
|
||||
IPAddress('::1')
|
||||
|
||||
>>> IPAddress(10)
|
||||
IPAddress('0.0.0.10')
|
||||
|
||||
>>> IPAddress(0x1ffffffff)
|
||||
IPAddress('::1:ffff:ffff')
|
||||
|
||||
>>> IPAddress(0xffffffff, 6)
|
||||
IPAddress('::255.255.255.255')
|
||||
|
||||
>>> IPAddress(0x1ffffffff)
|
||||
IPAddress('::1:ffff:ffff')
|
||||
|
||||
>>> IPAddress(2 ** 128 - 1)
|
||||
IPAddress('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress constructor - IPv4 inet_aton behaviour (default).
|
||||
|
||||
{{{
|
||||
|
||||
# Hexadecimal octets.
|
||||
>>> IPAddress('0x7f.0x1')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
>>> IPAddress('0x7f.0x0.0x0.0x1')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
# Octal octets.
|
||||
>>> IPAddress('0177.01')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
# Mixed octets.
|
||||
>>> IPAddress('0x7f.0.01')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
# Partial addresses - pretty weird ...
|
||||
>>> IPAddress('127')
|
||||
IPAddress('0.0.0.127')
|
||||
|
||||
>>> IPAddress('127')
|
||||
IPAddress('0.0.0.127')
|
||||
|
||||
>>> IPAddress('127.1')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
>>> IPAddress('127.0.1')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress constructor - IPv4 inet_pton behaviour (stricter parser).
|
||||
|
||||
{{{
|
||||
|
||||
# Octal octets.
|
||||
>>> IPAddress('0177.01', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: failed to detect a valid IP address from '0177.01'
|
||||
|
||||
# Mixed octets.
|
||||
>>> IPAddress('0x7f.0.01', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: failed to detect a valid IP address from '0x7f.0.01'
|
||||
|
||||
# Partial octets.
|
||||
>>> IPAddress('10', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: failed to detect a valid IP address from '10'
|
||||
|
||||
>>> IPAddress('10.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: failed to detect a valid IP address from '10.1'
|
||||
|
||||
>>> IPAddress('10.0.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: failed to detect a valid IP address from '10.0.1'
|
||||
|
||||
>>> IPAddress('10.0.0.1', flags=INET_PTON)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress constructor - zero filled octets.
|
||||
|
||||
{{{
|
||||
|
||||
# This takes a lot of people by surprise ...
|
||||
>>> IPAddress('010.000.000.001')
|
||||
IPAddress('8.0.0.1')
|
||||
|
||||
# So, we need this!
|
||||
>>> IPAddress('010.000.000.001', flags=ZEROFILL)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
# Zero-fill with inet_aton behaviour - partial octets are OK but zero-filled
|
||||
# octets are interpreted as decimal ...
|
||||
>>> IPAddress('010.000.001', flags=ZEROFILL)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
# Zero-fill with inet_pton behaviour - 4 octets only!
|
||||
>>> IPAddress('010.000.001', flags=INET_PTON|ZEROFILL)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: failed to detect a valid IP address from '010.000.001'
|
||||
|
||||
# Zero-fill with inet_pton behaviour - 4 octets only!
|
||||
>>> IPAddress('010.000.000.001', flags=INET_PTON|ZEROFILL)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
# To save some typing there are short versions of these flags.
|
||||
>>> IPAddress('010.000.000.001', flags=P|Z)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
}}}
|
||||
|
||||
IP network construction.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24')
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork('192.0.2.0/255.255.255.0')
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork('192.0.2.0/0.0.0.255')
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork(IPNetwork('192.0.2.0/24'))
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork(IPNetwork('::192.0.2.0/120'))
|
||||
IPNetwork('::192.0.2.0/120')
|
||||
|
||||
>>> IPNetwork(IPNetwork('192.0.2.0/24'))
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork('::192.0.2.0/120')
|
||||
IPNetwork('::192.0.2.0/120')
|
||||
|
||||
>>> IPNetwork('::192.0.2.0/120', 6)
|
||||
IPNetwork('::192.0.2.0/120')
|
||||
|
||||
}}}
|
||||
|
||||
Optional implicit IP network prefix selection rules.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0', implicit_prefix=True)
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork('231.192.0.15', implicit_prefix=True)
|
||||
IPNetwork('231.192.0.15/4')
|
||||
|
||||
>>> IPNetwork('10', implicit_prefix=True)
|
||||
IPNetwork('10.0.0.0/8')
|
||||
|
||||
}}}
|
||||
|
||||
Optional flags for tweaking IPNetwork constructor behaviour.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('172.24.200')
|
||||
IPNetwork('172.24.200.0/32')
|
||||
|
||||
>>> IPNetwork('172.24.200', implicit_prefix=True)
|
||||
IPNetwork('172.24.200.0/16')
|
||||
|
||||
# Truncate the host bits so we get a pure network.
|
||||
>>> IPNetwork('172.24.200', implicit_prefix=True, flags=NOHOST)
|
||||
IPNetwork('172.24.0.0/16')
|
||||
|
||||
|
||||
}}}
|
||||
|
||||
Negative testing
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('foo')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: invalid IPNetwork foo
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,27 @@
|
||||
=IP formatting options=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
==IPAddress representations==
|
||||
|
||||
{{{
|
||||
|
||||
>>> hex(IPAddress(0))
|
||||
'0x0'
|
||||
|
||||
>>> hex(IPAddress(0xffffffff))
|
||||
'0xffffffff'
|
||||
|
||||
>>> oct(IPAddress(0))
|
||||
'0'
|
||||
|
||||
>>> oct(IPAddress(0xffffffff))
|
||||
'037777777777'
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,48 @@
|
||||
=IP Function Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
During a cidr merge operation, the address 0.0.0.0/0, representing the whole of the IPv4 address space, should swallow anything it is merged with.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(['0.0.0.0/0', '0.0.0.0'])
|
||||
[IPNetwork('0.0.0.0/0')]
|
||||
|
||||
>>> cidr_merge(['0.0.0.0/0', '255.255.255.255'])
|
||||
[IPNetwork('0.0.0.0/0')]
|
||||
|
||||
>>> cidr_merge(['0.0.0.0/0', '192.0.2.0/24', '10.0.0.0/8'])
|
||||
[IPNetwork('0.0.0.0/0')]
|
||||
|
||||
}}}
|
||||
|
||||
Same goes for the IPv6 CIDR ::/0, representing the whole of the IPv6 address space.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(['::/0', 'fe80::1'])
|
||||
[IPNetwork('::/0')]
|
||||
|
||||
>>> cidr_merge(['::/0', '::'])
|
||||
[IPNetwork('::/0')]
|
||||
|
||||
>>> cidr_merge(['::/0', '::192.0.2.0/124', 'ff00::101'])
|
||||
[IPNetwork('::/0')]
|
||||
|
||||
}}}
|
||||
|
||||
This also applies to mixed IPv4 and IPv6 address lists.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(['0.0.0.0/0', '0.0.0.0', '::/0', '::'])
|
||||
[IPNetwork('0.0.0.0/0'), IPNetwork('::/0')]
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,80 @@
|
||||
=IntSet Tests=
|
||||
|
||||
Copyright (c) 2006, Heiko Wundram.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.ip.intset import IntSet
|
||||
|
||||
>>> x = IntSet((10, 20), 30)
|
||||
>>> y = IntSet((10, 20))
|
||||
>>> z = IntSet((10, 20), 30, (15, 19), min=0, max=40)
|
||||
|
||||
>>> x
|
||||
IntSet((10,20),30)
|
||||
|
||||
>>> x & 110
|
||||
IntSet()
|
||||
|
||||
>>> x | 110
|
||||
IntSet((10,20),30,110)
|
||||
|
||||
>>> x ^ (15, 25)
|
||||
IntSet((10,14),(21,25),30)
|
||||
|
||||
>>> x - 12
|
||||
IntSet((10,11),(13,20),30)
|
||||
|
||||
>>> 12 in x
|
||||
True
|
||||
|
||||
>>> x.issubset(x)
|
||||
True
|
||||
|
||||
>>> y.issubset(x)
|
||||
True
|
||||
|
||||
>>> x.istruesubset(x)
|
||||
False
|
||||
|
||||
>>> y.istruesubset(x)
|
||||
True
|
||||
|
||||
>>> for val in x:
|
||||
... val
|
||||
10
|
||||
11
|
||||
12
|
||||
13
|
||||
14
|
||||
15
|
||||
16
|
||||
17
|
||||
18
|
||||
19
|
||||
20
|
||||
30
|
||||
|
||||
|
||||
>>> x.inverse()
|
||||
IntSet((None,9),(21,29),(31,None))
|
||||
|
||||
>>> x == z
|
||||
True
|
||||
|
||||
>>> x == y
|
||||
False
|
||||
|
||||
>>> x != y
|
||||
True
|
||||
|
||||
>>> hash(x) == hash(z)
|
||||
True
|
||||
|
||||
>>> len(x)
|
||||
12
|
||||
|
||||
>>> x.len()
|
||||
12
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,72 @@
|
||||
=IP Glob Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
IP Glob tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_to_glob('10.0.0.1/32')
|
||||
'10.0.0.1'
|
||||
|
||||
>>> cidr_to_glob('192.0.2.0/24')
|
||||
'192.0.2.*'
|
||||
|
||||
>>> cidr_to_glob('172.16.0.0/12')
|
||||
'172.16-31.*.*'
|
||||
|
||||
>>> cidr_to_glob('0.0.0.0/0')
|
||||
'*.*.*.*'
|
||||
|
||||
>>> glob_to_cidrs('10.0.0.1')
|
||||
[IPNetwork('10.0.0.1/32')]
|
||||
|
||||
>>> glob_to_cidrs('192.0.2.*')
|
||||
[IPNetwork('192.0.2.0/24')]
|
||||
|
||||
>>> glob_to_cidrs('172.16-31.*.*')
|
||||
[IPNetwork('172.16.0.0/12')]
|
||||
|
||||
>>> glob_to_cidrs('*.*.*.*')
|
||||
[IPNetwork('0.0.0.0/0')]
|
||||
|
||||
>>> glob_to_iptuple('*.*.*.*')
|
||||
(IPAddress('0.0.0.0'), IPAddress('255.255.255.255'))
|
||||
|
||||
>>> iprange_to_globs('192.0.2.0', '192.0.2.255')
|
||||
['192.0.2.*']
|
||||
|
||||
>>> iprange_to_globs('192.0.2.1', '192.0.2.15')
|
||||
['192.0.2.1-15']
|
||||
|
||||
>>> iprange_to_globs('192.0.2.255', '192.0.4.1')
|
||||
['192.0.2.255', '192.0.3.*', '192.0.4.0-1']
|
||||
|
||||
>>> iprange_to_globs('10.0.1.255', '10.0.255.255')
|
||||
['10.0.1.255', '10.0.2-3.*', '10.0.4-7.*', '10.0.8-15.*', '10.0.16-31.*', '10.0.32-63.*', '10.0.64-127.*', '10.0.128-255.*']
|
||||
|
||||
}}}
|
||||
|
||||
Validity tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> valid_glob('1.1.1.a')
|
||||
False
|
||||
|
||||
>>> valid_glob('1.1.1.1/32')
|
||||
False
|
||||
|
||||
>>> valid_glob('1.1.1.a-b')
|
||||
False
|
||||
|
||||
>>> valid_glob('1.1.a-b.*')
|
||||
False
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,159 @@
|
||||
=IPRange Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Constructor tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('192.0.2.1', '192.0.2.254')
|
||||
|
||||
>>> iprange
|
||||
IPRange('192.0.2.1', '192.0.2.254')
|
||||
|
||||
>>> '%s' % iprange
|
||||
'192.0.2.1-192.0.2.254'
|
||||
|
||||
>>> IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')
|
||||
IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')
|
||||
|
||||
>>> IPRange('192.0.2.1', '192.0.2.1')
|
||||
IPRange('192.0.2.1', '192.0.2.1')
|
||||
|
||||
>>> IPRange('208.049.164.000', '208.050.066.255', flags=ZEROFILL)
|
||||
IPRange('208.49.164.0', '208.50.66.255')
|
||||
|
||||
}}}
|
||||
|
||||
Bad constructor tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPRange('192.0.2.2', '192.0.2.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: lower bound IP greater than upper bound!
|
||||
|
||||
>>> IPRange('::', '0.0.0.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: base address '0.0.0.1' is not IPv6
|
||||
|
||||
>>> IPRange('0.0.0.0', '::1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: base address '::1' is not IPv4
|
||||
|
||||
}}}
|
||||
|
||||
Indexing and slicing tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('192.0.2.1', '192.0.2.254')
|
||||
|
||||
>>> len(iprange)
|
||||
254
|
||||
|
||||
>>> iprange.first == 3221225985
|
||||
True
|
||||
|
||||
>>> iprange.last == 3221226238
|
||||
True
|
||||
|
||||
>>> iprange[0]
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> iprange[-1]
|
||||
IPAddress('192.0.2.254')
|
||||
|
||||
>>> iprange[512]
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: index out range for address range size!
|
||||
|
||||
>>> list(iprange[0:3])
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3')]
|
||||
|
||||
>>> list(iprange[0:10:2])
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.3'), IPAddress('192.0.2.5'), IPAddress('192.0.2.7'), IPAddress('192.0.2.9')]
|
||||
|
||||
>>> list(iprange[0:1024:512])
|
||||
[IPAddress('192.0.2.1')]
|
||||
|
||||
>>> IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')[0:10:2]
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: IPv6 slices are not supported!
|
||||
|
||||
}}}
|
||||
|
||||
Membership tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPRange('192.0.2.5', '192.0.2.10') in IPRange('192.0.2.1', '192.0.2.254')
|
||||
True
|
||||
|
||||
>>> IPRange('fe80::1', 'fe80::fffe') in IPRange('fe80::', 'fe80::ffff:ffff:ffff:ffff')
|
||||
True
|
||||
|
||||
>>> IPRange('192.0.2.5', '192.0.2.10') in IPRange('::', '::255.255.255.255')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Sorting tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ipranges = (IPRange('192.0.2.40', '192.0.2.50'), IPRange('192.0.2.20', '192.0.2.30'), IPRange('192.0.2.1', '192.0.2.254'),)
|
||||
|
||||
>>> sorted(ipranges)
|
||||
[IPRange('192.0.2.1', '192.0.2.254'), IPRange('192.0.2.20', '192.0.2.30'), IPRange('192.0.2.40', '192.0.2.50')]
|
||||
|
||||
>>> ipranges = list(ipranges)
|
||||
|
||||
>>> ipranges.append(IPRange('192.0.2.45', '192.0.2.49'))
|
||||
|
||||
>>> sorted(ipranges)
|
||||
[IPRange('192.0.2.1', '192.0.2.254'), IPRange('192.0.2.20', '192.0.2.30'), IPRange('192.0.2.40', '192.0.2.50'), IPRange('192.0.2.45', '192.0.2.49')]
|
||||
|
||||
}}}
|
||||
|
||||
CIDR interoperability tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPRange('192.0.2.5', '192.0.2.10').cidrs()
|
||||
[IPNetwork('192.0.2.5/32'), IPNetwork('192.0.2.6/31'), IPNetwork('192.0.2.8/31'), IPNetwork('192.0.2.10/32')]
|
||||
|
||||
>>> IPRange('fe80::', 'fe80::ffff:ffff:ffff:ffff').cidrs()
|
||||
[IPNetwork('fe80::/64')]
|
||||
|
||||
}}}
|
||||
|
||||
Various additional tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange.info
|
||||
{'IPv4': [{'date': '1993-05',
|
||||
'designation': 'Administered by ARIN',
|
||||
'prefix': '192/8',
|
||||
'status': 'Legacy',
|
||||
'whois': 'whois.arin.net'}]}
|
||||
|
||||
>>> iprange.is_private()
|
||||
True
|
||||
|
||||
>>> iprange.version
|
||||
4
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,65 @@
|
||||
=IP Matching Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', ['192.0.2.0'])
|
||||
IPNetwork('192.0.2.0/32')
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0'])
|
||||
IPNetwork('192.0.2.0/32')
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0', '224.0.0.1'])
|
||||
IPNetwork('192.0.2.0/32')
|
||||
|
||||
>>> smallest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0', '224.0.0.1'])
|
||||
IPNetwork('192.0.2.0/32')
|
||||
|
||||
>>> smallest_matching_cidr('192.0.2.0', ['10.0.0.1', '224.0.0.1'])
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '224.0.0.1'])
|
||||
|
||||
>>> networks = [str(c) for c in IPNetwork('192.0.2.128/27').supernet(22)]
|
||||
|
||||
>>> networks
|
||||
['192.0.0.0/22', '192.0.2.0/23', '192.0.2.0/24', '192.0.2.128/25', '192.0.2.128/26']
|
||||
|
||||
>>> all_matching_cidrs('192.0.2.0', networks)
|
||||
[IPNetwork('192.0.0.0/22'), IPNetwork('192.0.2.0/23'), IPNetwork('192.0.2.0/24')]
|
||||
|
||||
>>> smallest_matching_cidr('192.0.2.0', networks)
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', networks)
|
||||
IPNetwork('192.0.0.0/22')
|
||||
|
||||
}}}
|
||||
|
||||
Checking matches with varying IP address versions.
|
||||
|
||||
{{{
|
||||
>>> all_matching_cidrs('192.0.2.0', ['192.0.2.0/24'])
|
||||
[IPNetwork('192.0.2.0/24')]
|
||||
|
||||
>>> all_matching_cidrs('192.0.2.0', ['::/96'])
|
||||
[]
|
||||
|
||||
>>> all_matching_cidrs('::ffff:192.0.2.1', ['::ffff:192.0.2.0/96'])
|
||||
[IPNetwork('::ffff:192.0.2.0/96')]
|
||||
|
||||
>>> all_matching_cidrs('::192.0.2.1', ['::192.0.2.0/96'])
|
||||
[IPNetwork('::192.0.2.0/96')]
|
||||
|
||||
>>> all_matching_cidrs('::192.0.2.1', ['192.0.2.0/23'])
|
||||
[]
|
||||
|
||||
>>> all_matching_cidrs('::192.0.2.1', ['192.0.2.0/24', '::192.0.2.0/120'])
|
||||
[IPNetwork('::192.0.2.0/120')]
|
||||
|
||||
>>> all_matching_cidrs('::192.0.2.1', [IPNetwork('192.0.2.0/24'), IPNetwork('::192.0.2.0/120')])
|
||||
[IPNetwork('::192.0.2.0/120')]
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,30 @@
|
||||
=IP Multicast Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
>>> ip = IPAddress('239.192.0.1')
|
||||
|
||||
>>> ip.is_multicast()
|
||||
True
|
||||
|
||||
>>> ip = IPAddress(3221225984)
|
||||
|
||||
>>> ip = IPAddress('224.0.1.173')
|
||||
|
||||
>>> ip.info.IPv4[0].designation
|
||||
'Multicast'
|
||||
|
||||
>>> ip.info.IPv4[0].prefix
|
||||
'224/8'
|
||||
|
||||
>>> ip.info.IPv4[0].status
|
||||
'Reserved'
|
||||
|
||||
>>> ip.info.Multicast[0].address
|
||||
'224.0.1.173'
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,114 @@
|
||||
=nmap IP Range Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
nmap IP range validation.
|
||||
|
||||
{{{
|
||||
|
||||
>>> valid_nmap_range('192.0.2.1')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('192.0.2.0-31')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('192.0.2-3.1-254')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.0-255')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('192.168.3-5,7.1')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('192.168.3-5,7,10-12,13,14.1')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range(1)
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('1')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range([])
|
||||
False
|
||||
|
||||
>>> valid_nmap_range({})
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('::')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('255.255.255.256')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.0-256')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.-1-0')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.256-0')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.255-0')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('a.b.c.d-e')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('255.255.255.a-b')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
nmap IP range iteration.
|
||||
|
||||
{{{
|
||||
|
||||
>>> list(iter_nmap_range('192.0.2.1'))
|
||||
[IPAddress('192.0.2.1')]
|
||||
|
||||
>>> ip_list = list(iter_nmap_range('192.0.2.0-31'))
|
||||
>>> len(ip_list)
|
||||
32
|
||||
>>> ip_list
|
||||
[IPAddress('192.0.2.0'), IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15'), IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), IPAddress('192.0.2.18'), IPAddress('192.0.2.19'), IPAddress('192.0.2.20'), IPAddress('192.0.2.21'), IPAddress('192.0.2.22'), IPAddress('192.0.2.23'), IPAddress('192.0.2.24'), IPAddress('192.0.2.25'), IPAddress('192.0.2.26'), IPAddress('192.0.2.27'), IPAddress('192.0.2.28'), IPAddress('192.0.2.29'), IPAddress('192.0.2.30'), IPAddress('192.0.2.31')]
|
||||
|
||||
>>> ip_list = list(iter_nmap_range('192.0.2-3.1-7'))
|
||||
>>> len(ip_list)
|
||||
14
|
||||
|
||||
>>> list(iter_nmap_range('192.0.2.1-3,5,7-9'))
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.5'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9')]
|
||||
|
||||
>>> for ip in ip_list:
|
||||
... print ip
|
||||
...
|
||||
192.0.2.1
|
||||
192.0.2.2
|
||||
192.0.2.3
|
||||
192.0.2.4
|
||||
192.0.2.5
|
||||
192.0.2.6
|
||||
192.0.2.7
|
||||
192.0.3.1
|
||||
192.0.3.2
|
||||
192.0.3.3
|
||||
192.0.3.4
|
||||
192.0.3.5
|
||||
192.0.3.6
|
||||
192.0.3.7
|
||||
|
||||
>>> list(iter_nmap_range('::'))
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: invalid nmap range: ::
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,214 @@
|
||||
=IP Persistence Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
>>> import pickle
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress object pickling - IPv4.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress(3221225985)
|
||||
>>> ip
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> buf = pickle.dumps(ip)
|
||||
|
||||
>>> ip2 = pickle.loads(buf)
|
||||
|
||||
>>> ip2 == ip
|
||||
True
|
||||
|
||||
>>> id(ip2) != id(ip)
|
||||
True
|
||||
|
||||
>>> ip2.value == 3221225985
|
||||
True
|
||||
|
||||
>>> ip2.version
|
||||
4
|
||||
|
||||
>>> del ip, buf, ip2
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress object pickling - IPv6.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress('::ffff:192.0.2.1')
|
||||
|
||||
>>> ip
|
||||
IPAddress('::ffff:192.0.2.1')
|
||||
|
||||
>>> ip.value == 281473902969345
|
||||
True
|
||||
|
||||
>>> buf = pickle.dumps(ip)
|
||||
|
||||
>>> ip2 = pickle.loads(buf)
|
||||
|
||||
>>> ip2 == ip
|
||||
True
|
||||
|
||||
>>> ip2.value == 281473902969345
|
||||
True
|
||||
|
||||
>>> ip2.version
|
||||
6
|
||||
|
||||
>>> del ip, buf, ip2
|
||||
|
||||
}}}
|
||||
|
||||
IPNetwork pickling - IPv4.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr = IPNetwork('192.0.2.0/24')
|
||||
>>> cidr
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> buf = pickle.dumps(cidr)
|
||||
|
||||
>>> cidr2 = pickle.loads(buf)
|
||||
|
||||
>>> cidr2 == cidr
|
||||
True
|
||||
|
||||
>>> id(cidr2) != id(cidr)
|
||||
True
|
||||
|
||||
>>> cidr2.value == 3221225984
|
||||
True
|
||||
|
||||
>>> cidr2.prefixlen
|
||||
24
|
||||
|
||||
>>> cidr2.version
|
||||
4
|
||||
|
||||
>>> del cidr, buf, cidr2
|
||||
|
||||
}}}
|
||||
|
||||
IPNetwork object pickling - IPv6.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr = IPNetwork('::ffff:192.0.2.0/120')
|
||||
|
||||
>>> cidr
|
||||
IPNetwork('::ffff:192.0.2.0/120')
|
||||
|
||||
>>> cidr.value == 281473902969344
|
||||
True
|
||||
|
||||
>>> cidr.prefixlen
|
||||
120
|
||||
|
||||
>>> buf = pickle.dumps(cidr)
|
||||
|
||||
>>> cidr2 = pickle.loads(buf)
|
||||
|
||||
>>> cidr2 == cidr
|
||||
True
|
||||
|
||||
>>> cidr2.value == 281473902969344
|
||||
True
|
||||
|
||||
>>> cidr2.prefixlen
|
||||
120
|
||||
|
||||
>>> cidr2.version
|
||||
6
|
||||
|
||||
>>> del cidr, buf, cidr2
|
||||
|
||||
}}}
|
||||
|
||||
}}}
|
||||
|
||||
IPRange object pickling - IPv4.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('192.0.2.1', '192.0.2.254')
|
||||
>>> iprange
|
||||
IPRange('192.0.2.1', '192.0.2.254')
|
||||
|
||||
>>> iprange.first == 3221225985
|
||||
True
|
||||
|
||||
>>> iprange.last == 3221226238
|
||||
True
|
||||
|
||||
>>> iprange.version
|
||||
4
|
||||
|
||||
>>> buf = pickle.dumps(iprange)
|
||||
|
||||
>>> iprange2 = pickle.loads(buf)
|
||||
|
||||
>>> iprange2 == iprange
|
||||
True
|
||||
|
||||
>>> id(iprange2) != id(iprange)
|
||||
True
|
||||
|
||||
>>> iprange2.first == 3221225985
|
||||
True
|
||||
|
||||
>>> iprange2.last == 3221226238
|
||||
True
|
||||
|
||||
>>> iprange2.version
|
||||
4
|
||||
|
||||
>>> del iprange, buf, iprange2
|
||||
|
||||
}}}
|
||||
|
||||
IPRange object pickling - IPv6.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')
|
||||
|
||||
>>> iprange
|
||||
IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')
|
||||
|
||||
>>> iprange.first == 281473902969345
|
||||
True
|
||||
|
||||
>>> iprange.last == 281473902969598
|
||||
True
|
||||
|
||||
>>> iprange.version
|
||||
6
|
||||
|
||||
>>> buf = pickle.dumps(iprange)
|
||||
|
||||
>>> iprange2 = pickle.loads(buf)
|
||||
|
||||
>>> iprange2 == iprange
|
||||
True
|
||||
|
||||
>>> iprange2.first == 281473902969345
|
||||
True
|
||||
|
||||
>>> iprange2.last == 281473902969598
|
||||
True
|
||||
|
||||
>>> iprange2.version
|
||||
6
|
||||
|
||||
>>> del iprange, buf, iprange2
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
=Mac OSX Specific Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 compatible IPv6 range to CIDR.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('::1', '::255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
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')
|
||||
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')
|
||||
IPNetwork('::240.0.0.0/101')
|
||||
IPNetwork('::248.0.0.0/102')
|
||||
IPNetwork('::252.0.0.0/103')
|
||||
IPNetwork('::254.0.0.0/104')
|
||||
IPNetwork('::255.0.0.0/105')
|
||||
IPNetwork('::255.128.0.0/106')
|
||||
IPNetwork('::255.192.0.0/107')
|
||||
IPNetwork('::255.224.0.0/108')
|
||||
IPNetwork('::255.240.0.0/109')
|
||||
IPNetwork('::255.248.0.0/110')
|
||||
IPNetwork('::255.252.0.0/111')
|
||||
IPNetwork('::255.254.0.0/112')
|
||||
IPNetwork('::255.255.0.0/113')
|
||||
IPNetwork('::255.255.128.0/114')
|
||||
IPNetwork('::255.255.192.0/115')
|
||||
IPNetwork('::255.255.224.0/116')
|
||||
IPNetwork('::255.255.240.0/117')
|
||||
IPNetwork('::255.255.248.0/118')
|
||||
IPNetwork('::255.255.252.0/119')
|
||||
IPNetwork('::255.255.254.0/120')
|
||||
IPNetwork('::255.255.255.0/121')
|
||||
IPNetwork('::255.255.255.128/122')
|
||||
IPNetwork('::255.255.255.192/123')
|
||||
IPNetwork('::255.255.255.224/124')
|
||||
IPNetwork('::255.255.255.240/125')
|
||||
IPNetwork('::255.255.255.248/126')
|
||||
IPNetwork('::255.255.255.252/127')
|
||||
IPNetwork('::255.255.255.254/128')
|
||||
|
||||
# inet_pton has to be different on Mac OSX *sigh*
|
||||
>>> IPAddress('010.000.000.001', flags=INET_PTON)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
>>> from netaddr.strategy.ipv6 import int_to_str
|
||||
>>> int_to_str(0xffff)
|
||||
'::0.0.255.255'
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
=Linux Specific Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 compatible IPv6 range to CIDR.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('::1', '::255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
IPNetwork('::1/128')
|
||||
IPNetwork('::2/127')
|
||||
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')
|
||||
IPNetwork('::240.0.0.0/101')
|
||||
IPNetwork('::248.0.0.0/102')
|
||||
IPNetwork('::252.0.0.0/103')
|
||||
IPNetwork('::254.0.0.0/104')
|
||||
IPNetwork('::255.0.0.0/105')
|
||||
IPNetwork('::255.128.0.0/106')
|
||||
IPNetwork('::255.192.0.0/107')
|
||||
IPNetwork('::255.224.0.0/108')
|
||||
IPNetwork('::255.240.0.0/109')
|
||||
IPNetwork('::255.248.0.0/110')
|
||||
IPNetwork('::255.252.0.0/111')
|
||||
IPNetwork('::255.254.0.0/112')
|
||||
IPNetwork('::255.255.0.0/113')
|
||||
IPNetwork('::255.255.128.0/114')
|
||||
IPNetwork('::255.255.192.0/115')
|
||||
IPNetwork('::255.255.224.0/116')
|
||||
IPNetwork('::255.255.240.0/117')
|
||||
IPNetwork('::255.255.248.0/118')
|
||||
IPNetwork('::255.255.252.0/119')
|
||||
IPNetwork('::255.255.254.0/120')
|
||||
IPNetwork('::255.255.255.0/121')
|
||||
IPNetwork('::255.255.255.128/122')
|
||||
IPNetwork('::255.255.255.192/123')
|
||||
IPNetwork('::255.255.255.224/124')
|
||||
IPNetwork('::255.255.255.240/125')
|
||||
IPNetwork('::255.255.255.248/126')
|
||||
IPNetwork('::255.255.255.252/127')
|
||||
IPNetwork('::255.255.255.254/128')
|
||||
|
||||
# Sadly, inet_pton cannot help us here ...
|
||||
>>> IPAddress('010.000.000.001', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: failed to detect a valid IP address from '010.000.000.001'
|
||||
|
||||
>>> from netaddr.strategy.ipv6 import int_to_str
|
||||
>>> int_to_str(0xffff)
|
||||
'::ffff'
|
||||
|
||||
}}}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
=Windows Specific Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 compatible IPv6 range to CIDR.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('::1', '::255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
IPNetwork('::1/128')
|
||||
IPNetwork('::2/127')
|
||||
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')
|
||||
IPNetwork('::240.0.0.0/101')
|
||||
IPNetwork('::248.0.0.0/102')
|
||||
IPNetwork('::252.0.0.0/103')
|
||||
IPNetwork('::254.0.0.0/104')
|
||||
IPNetwork('::255.0.0.0/105')
|
||||
IPNetwork('::255.128.0.0/106')
|
||||
IPNetwork('::255.192.0.0/107')
|
||||
IPNetwork('::255.224.0.0/108')
|
||||
IPNetwork('::255.240.0.0/109')
|
||||
IPNetwork('::255.248.0.0/110')
|
||||
IPNetwork('::255.252.0.0/111')
|
||||
IPNetwork('::255.254.0.0/112')
|
||||
IPNetwork('::255.255.0.0/113')
|
||||
IPNetwork('::255.255.128.0/114')
|
||||
IPNetwork('::255.255.192.0/115')
|
||||
IPNetwork('::255.255.224.0/116')
|
||||
IPNetwork('::255.255.240.0/117')
|
||||
IPNetwork('::255.255.248.0/118')
|
||||
IPNetwork('::255.255.252.0/119')
|
||||
IPNetwork('::255.255.254.0/120')
|
||||
IPNetwork('::255.255.255.0/121')
|
||||
IPNetwork('::255.255.255.128/122')
|
||||
IPNetwork('::255.255.255.192/123')
|
||||
IPNetwork('::255.255.255.224/124')
|
||||
IPNetwork('::255.255.255.240/125')
|
||||
IPNetwork('::255.255.255.248/126')
|
||||
IPNetwork('::255.255.255.252/127')
|
||||
IPNetwork('::255.255.255.254/128')
|
||||
|
||||
# Sadly, inet_pton cannot help us here ...
|
||||
>>> IPAddress('010.000.000.001', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: failed to detect a valid IP address from '010.000.000.001'
|
||||
|
||||
>>> from netaddr.strategy.ipv6 import int_to_str
|
||||
>>> int_to_str(0xffff)
|
||||
'::ffff'
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
=RFC 1924 Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
The example from the RFC.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.ip.rfc1924 import ipv6_to_base85, base85_to_ipv6
|
||||
|
||||
>>> ip_addr = '1080::8:800:200c:417a'
|
||||
|
||||
>>> ip_addr
|
||||
'1080::8:800:200c:417a'
|
||||
|
||||
>>> base85 = ipv6_to_base85(ip_addr)
|
||||
|
||||
>>> base85
|
||||
'4)+k&C#VzJ4br>0wv%Yp'
|
||||
|
||||
>>> base85_to_ipv6(base85)
|
||||
'1080::8:800:200c:417a'
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,414 @@
|
||||
First of all you need to pull the various netaddr classes and functions into your namespace.
|
||||
|
||||
.. note:: Do this for the purpose of this tutorial only. In your own code, you should be explicit about the classes, functions and constants you import to avoid name clashes.
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
----------------
|
||||
Creating IP sets
|
||||
----------------
|
||||
|
||||
Here how to create IP sets.
|
||||
|
||||
An empty set.
|
||||
|
||||
>>> IPSet()
|
||||
IPSet([])
|
||||
>>> IPSet([])
|
||||
IPSet([])
|
||||
|
||||
You can specific either IP addresses and networks as strings, or as `IPAddress` or `IPNetwork` objects.
|
||||
|
||||
>>> IPSet(['192.0.2.0'])
|
||||
IPSet(['192.0.2.0/32'])
|
||||
>>> IPSet([IPAddress('192.0.2.0')])
|
||||
IPSet(['192.0.2.0/32'])
|
||||
>>> IPSet([IPNetwork('192.0.2.0')])
|
||||
IPSet(['192.0.2.0/32'])
|
||||
>>> IPSet([IPNetwork('192.0.2.0/24')])
|
||||
IPSet(['192.0.2.0/24'])
|
||||
|
||||
You can interate over all the IP addresses that are members of IP set.
|
||||
|
||||
>>> for ip in IPSet(['192.0.2.0/28', '::192.0.2.0/124']):
|
||||
... print ip
|
||||
192.0.2.0
|
||||
192.0.2.1
|
||||
192.0.2.2
|
||||
192.0.2.3
|
||||
192.0.2.4
|
||||
192.0.2.5
|
||||
192.0.2.6
|
||||
192.0.2.7
|
||||
192.0.2.8
|
||||
192.0.2.9
|
||||
192.0.2.10
|
||||
192.0.2.11
|
||||
192.0.2.12
|
||||
192.0.2.13
|
||||
192.0.2.14
|
||||
192.0.2.15
|
||||
::192.0.2.0
|
||||
::192.0.2.1
|
||||
::192.0.2.2
|
||||
::192.0.2.3
|
||||
::192.0.2.4
|
||||
::192.0.2.5
|
||||
::192.0.2.6
|
||||
::192.0.2.7
|
||||
::192.0.2.8
|
||||
::192.0.2.9
|
||||
::192.0.2.10
|
||||
::192.0.2.11
|
||||
::192.0.2.12
|
||||
::192.0.2.13
|
||||
::192.0.2.14
|
||||
::192.0.2.15
|
||||
|
||||
--------------------------------
|
||||
Adding and removing set elements
|
||||
--------------------------------
|
||||
|
||||
>>> s1 = IPSet()
|
||||
>>> s1.add('192.0.2.0')
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/32'])
|
||||
>>> s1.remove('192.0.2.0')
|
||||
>>> s1
|
||||
IPSet([])
|
||||
|
||||
--------------
|
||||
Set membership
|
||||
--------------
|
||||
|
||||
Here is a simple arbitrary IP address range.
|
||||
|
||||
>>> iprange = IPRange('192.0.1.255', '192.0.2.16')
|
||||
|
||||
We can see the CIDR networks that can existing with this defined range.
|
||||
|
||||
>>> iprange.cidrs()
|
||||
[IPNetwork('192.0.1.255/32'), IPNetwork('192.0.2.0/28'), IPNetwork('192.0.2.16/32')]
|
||||
|
||||
Here's an IP set.
|
||||
|
||||
>>> ipset = IPSet(['192.0.2.0/28'])
|
||||
|
||||
Now, let's iterate over the IP addresses in the arbitrary IP address range and see if they are found within the IP set.
|
||||
|
||||
>>> for ip in iprange:
|
||||
... print ip, ip in ipset
|
||||
192.0.1.255 False
|
||||
192.0.2.0 True
|
||||
192.0.2.1 True
|
||||
192.0.2.2 True
|
||||
192.0.2.3 True
|
||||
192.0.2.4 True
|
||||
192.0.2.5 True
|
||||
192.0.2.6 True
|
||||
192.0.2.7 True
|
||||
192.0.2.8 True
|
||||
192.0.2.9 True
|
||||
192.0.2.10 True
|
||||
192.0.2.11 True
|
||||
192.0.2.12 True
|
||||
192.0.2.13 True
|
||||
192.0.2.14 True
|
||||
192.0.2.15 True
|
||||
192.0.2.16 False
|
||||
|
||||
-------------------------------------
|
||||
Unions, intersections and differences
|
||||
-------------------------------------
|
||||
|
||||
Here are some examples of union operations performed on `IPSet` objects.
|
||||
|
||||
>>> IPSet(['192.0.2.0'])
|
||||
IPSet(['192.0.2.0/32'])
|
||||
|
||||
>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1'])
|
||||
IPSet(['192.0.2.0/31'])
|
||||
|
||||
>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3'])
|
||||
IPSet(['192.0.2.0/31', '192.0.2.3/32'])
|
||||
|
||||
>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3/30'])
|
||||
IPSet(['192.0.2.0/30'])
|
||||
|
||||
>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3/31'])
|
||||
IPSet(['192.0.2.0/30'])
|
||||
|
||||
>>> IPSet(['192.0.2.0/24']) | IPSet(['192.0.3.0/24']) | IPSet(['192.0.4.0/24'])
|
||||
IPSet(['192.0.2.0/23', '192.0.4.0/24'])
|
||||
|
||||
Here is an example of the union, intersection and symmetric difference operations all in play at the same time.
|
||||
|
||||
>>> adj_cidrs = list(IPNetwork('192.0.2.0/24').subnet(28))
|
||||
>>> even_cidrs = adj_cidrs[::2]
|
||||
>>> evens = IPSet(even_cidrs)
|
||||
>>> evens
|
||||
IPSet(['192.0.2.0/28', '192.0.2.32/28', '192.0.2.64/28', '192.0.2.96/28', '192.0.2.128/28', '192.0.2.160/28', '192.0.2.192/28', '192.0.2.224/28'])
|
||||
>>> IPSet(['192.0.2.0/24']) & evens
|
||||
IPSet(['192.0.2.0/28', '192.0.2.32/28', '192.0.2.64/28', '192.0.2.96/28', '192.0.2.128/28', '192.0.2.160/28', '192.0.2.192/28', '192.0.2.224/28'])
|
||||
>>> odds = IPSet(['192.0.2.0/24']) ^ evens
|
||||
>>> odds
|
||||
IPSet(['192.0.2.16/28', '192.0.2.48/28', '192.0.2.80/28', '192.0.2.112/28', '192.0.2.144/28', '192.0.2.176/28', '192.0.2.208/28', '192.0.2.240/28'])
|
||||
>>> evens | odds
|
||||
IPSet(['192.0.2.0/24'])
|
||||
>>> evens & odds
|
||||
IPSet([])
|
||||
>>> evens ^ odds
|
||||
IPSet(['192.0.2.0/24'])
|
||||
|
||||
---------------------
|
||||
Supersets and subsets
|
||||
---------------------
|
||||
|
||||
IP sets provide the ability to test whether a group of addresses ranges fit within the set of another group of address ranges.
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0/24', '192.0.4.0/24'])
|
||||
>>> s2 = IPSet(['192.0.2.0', '192.0.4.0'])
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/24', '192.0.4.0/24'])
|
||||
>>> s2
|
||||
IPSet(['192.0.2.0/32', '192.0.4.0/32'])
|
||||
>>> s1.issuperset(s2)
|
||||
True
|
||||
>>> s2.issubset(s1)
|
||||
True
|
||||
>>> s2.issuperset(s1)
|
||||
False
|
||||
>>> s1.issubset(s2)
|
||||
False
|
||||
|
||||
Here's a more complete example using various well known IPv4 address ranges.
|
||||
|
||||
>>> 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
|
||||
|
||||
Let's see what we've got:
|
||||
|
||||
>>> for cidr in available.iter_cidrs():
|
||||
... print cidr, cidr[0], cidr[-1]
|
||||
0.0.0.0/5 0.0.0.0 7.255.255.255
|
||||
8.0.0.0/7 8.0.0.0 9.255.255.255
|
||||
11.0.0.0/8 11.0.0.0 11.255.255.255
|
||||
12.0.0.0/6 12.0.0.0 15.255.255.255
|
||||
16.0.0.0/4 16.0.0.0 31.255.255.255
|
||||
32.0.0.0/3 32.0.0.0 63.255.255.255
|
||||
64.0.0.0/2 64.0.0.0 127.255.255.255
|
||||
128.0.0.0/3 128.0.0.0 159.255.255.255
|
||||
160.0.0.0/5 160.0.0.0 167.255.255.255
|
||||
168.0.0.0/6 168.0.0.0 171.255.255.255
|
||||
172.0.0.0/12 172.0.0.0 172.15.255.255
|
||||
172.32.0.0/11 172.32.0.0 172.63.255.255
|
||||
172.64.0.0/10 172.64.0.0 172.127.255.255
|
||||
172.128.0.0/9 172.128.0.0 172.255.255.255
|
||||
173.0.0.0/8 173.0.0.0 173.255.255.255
|
||||
174.0.0.0/7 174.0.0.0 175.255.255.255
|
||||
176.0.0.0/4 176.0.0.0 191.255.255.255
|
||||
192.0.0.0/23 192.0.0.0 192.0.1.255
|
||||
192.0.3.0/24 192.0.3.0 192.0.3.255
|
||||
192.0.4.0/22 192.0.4.0 192.0.7.255
|
||||
192.0.8.0/21 192.0.8.0 192.0.15.255
|
||||
192.0.16.0/20 192.0.16.0 192.0.31.255
|
||||
192.0.32.0/19 192.0.32.0 192.0.63.255
|
||||
192.0.64.0/18 192.0.64.0 192.0.127.255
|
||||
192.0.128.0/17 192.0.128.0 192.0.255.255
|
||||
192.1.0.0/16 192.1.0.0 192.1.255.255
|
||||
192.2.0.0/15 192.2.0.0 192.3.255.255
|
||||
192.4.0.0/14 192.4.0.0 192.7.255.255
|
||||
192.8.0.0/13 192.8.0.0 192.15.255.255
|
||||
192.16.0.0/12 192.16.0.0 192.31.255.255
|
||||
192.32.0.0/11 192.32.0.0 192.63.255.255
|
||||
192.64.0.0/10 192.64.0.0 192.127.255.255
|
||||
192.128.0.0/11 192.128.0.0 192.159.255.255
|
||||
192.160.0.0/13 192.160.0.0 192.167.255.255
|
||||
192.169.0.0/16 192.169.0.0 192.169.255.255
|
||||
192.170.0.0/15 192.170.0.0 192.171.255.255
|
||||
192.172.0.0/14 192.172.0.0 192.175.255.255
|
||||
192.176.0.0/12 192.176.0.0 192.191.255.255
|
||||
192.192.0.0/10 192.192.0.0 192.255.255.255
|
||||
193.0.0.0/8 193.0.0.0 193.255.255.255
|
||||
194.0.0.0/7 194.0.0.0 195.255.255.255
|
||||
196.0.0.0/6 196.0.0.0 199.255.255.255
|
||||
200.0.0.0/5 200.0.0.0 207.255.255.255
|
||||
208.0.0.0/4 208.0.0.0 223.255.255.255
|
||||
224.0.0.0/8 224.0.0.0 224.255.255.255
|
||||
232.0.0.0/7 232.0.0.0 233.255.255.255
|
||||
239.0.0.0/9 239.0.0.0 239.127.255.255
|
||||
239.128.0.0/10 239.128.0.0 239.191.255.255
|
||||
239.196.0.0/14 239.196.0.0 239.199.255.255
|
||||
239.200.0.0/13 239.200.0.0 239.207.255.255
|
||||
239.208.0.0/12 239.208.0.0 239.223.255.255
|
||||
239.224.0.0/11 239.224.0.0 239.255.255.255
|
||||
|
||||
>>> ipv4_addr_space ^ available
|
||||
IPSet(['10.0.0.0/8', '172.16.0.0/12', '192.0.2.0/24', '192.168.0.0/16', '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', '239.192.0.0/14', '240.0.0.0/4'])
|
||||
|
||||
|
||||
------------------------------
|
||||
Combined IPv4 and IPv6 support
|
||||
------------------------------
|
||||
|
||||
In keeping with netaddr's pragmatic approach, you are free to mix and match IPv4 and IPv6 within the same data structure.
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0', '::192.0.2.0', '192.0.2.2', '::192.0.2.2'])
|
||||
>>> s2 = IPSet(['192.0.2.2', '::192.0.2.2', '192.0.2.4', '::192.0.2.4'])
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/32', '192.0.2.2/32', '::192.0.2.0/128', '::192.0.2.2/128'])
|
||||
>>> s2
|
||||
IPSet(['192.0.2.2/32', '192.0.2.4/32', '::192.0.2.2/128', '::192.0.2.4/128'])
|
||||
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
IPv4 and IPv6 set union
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
>>> s1 | s2
|
||||
IPSet(['192.0.2.0/32', '192.0.2.2/32', '192.0.2.4/32', '::192.0.2.0/128', '::192.0.2.2/128', '::192.0.2.4/128'])
|
||||
|
||||
^^^^^^^^^^^^^^^^
|
||||
set intersection
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
||||
>>> s1 & s2
|
||||
IPSet(['192.0.2.2/32', '::192.0.2.2/128'])
|
||||
|
||||
^^^^^^^^^^^^^^
|
||||
set difference
|
||||
^^^^^^^^^^^^^^
|
||||
|
||||
>>> s1 - s2
|
||||
IPSet(['192.0.2.0/32', '::192.0.2.0/128'])
|
||||
>>> s2 - s1
|
||||
IPSet(['192.0.2.4/32', '::192.0.2.4/128'])
|
||||
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
set symmetric difference
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
>>> s1 ^ s2
|
||||
IPSet(['192.0.2.0/32', '192.0.2.4/32', '::192.0.2.0/128', '::192.0.2.4/128'])
|
||||
|
||||
------------------
|
||||
Disjointed IP sets
|
||||
------------------
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0', '192.0.2.1', '192.0.2.2'])
|
||||
>>> s2 = IPSet(['192.0.2.2', '192.0.2.3', '192.0.2.4'])
|
||||
>>> s1 & s2
|
||||
IPSet(['192.0.2.2/32'])
|
||||
>>> s1.isdisjoint(s2)
|
||||
False
|
||||
>>> s1 = IPSet(['192.0.2.0', '192.0.2.1'])
|
||||
>>> s2 = IPSet(['192.0.2.3', '192.0.2.4'])
|
||||
>>> s1 & s2
|
||||
IPSet([])
|
||||
>>> s1.isdisjoint(s2)
|
||||
True
|
||||
|
||||
------------------
|
||||
Updating an IP set
|
||||
------------------
|
||||
|
||||
As with a normal Python set you can also update one IP set with the contents of another.
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0/25'])
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/25'])
|
||||
>>> s2 = IPSet(['192.0.2.128/25'])
|
||||
>>> s2
|
||||
IPSet(['192.0.2.128/25'])
|
||||
>>> s1.update(s2)
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/24'])
|
||||
>>> s1.update(['192.0.0.0/24', '192.0.1.0/24', '192.0.3.0/24'])
|
||||
>>> s1
|
||||
IPSet(['192.0.0.0/22'])
|
||||
|
||||
|
||||
--------------------------------
|
||||
Removing elements from an IP set
|
||||
--------------------------------
|
||||
|
||||
Removing an IP address from an IPSet will the CIDR subnets within it into their constituent parts.
|
||||
|
||||
Here we create a set representing the entire IPv4 address space.
|
||||
|
||||
>>> s1 = IPSet(['0.0.0.0/0'])
|
||||
>>> s1
|
||||
IPSet(['0.0.0.0/0'])
|
||||
|
||||
Then we strip off the last address.
|
||||
|
||||
>>> s1.remove('255.255.255.255')
|
||||
|
||||
Leaving us with:
|
||||
|
||||
>>> s1
|
||||
IPSet(['0.0.0.0/1', '128.0.0.0/2', ..., '255.255.255.252/31', '255.255.255.254/32'])
|
||||
>>> list(s1.iter_cidrs())
|
||||
[IPNetwork('0.0.0.0/1'), IPNetwork('128.0.0.0/2'), ..., IPNetwork('255.255.255.252/31'), IPNetwork('255.255.255.254/32')]
|
||||
>>> len(list(s1.iter_cidrs()))
|
||||
32
|
||||
|
||||
Let's check the result using the `cidr_exclude` function.
|
||||
|
||||
>>> list(s1.iter_cidrs()) == cidr_exclude('0.0.0.0/0', '255.255.255.255')
|
||||
True
|
||||
|
||||
Next, let's remove the first address from the original range.
|
||||
|
||||
>>> s1.remove('0.0.0.0')
|
||||
|
||||
This fractures the CIDR subnets further.
|
||||
|
||||
>>> s1
|
||||
IPSet(['0.0.0.1/32', '0.0.0.2/31', ..., '255.255.255.252/31', '255.255.255.254/32'])
|
||||
>>> len(list(s1.iter_cidrs()))
|
||||
62
|
||||
|
||||
You can keep doing this but be aware that large IP sets can take up a lot of memory if they contain many thousands of entries.
|
||||
|
||||
----------------------------
|
||||
Adding elements to an IP set
|
||||
----------------------------
|
||||
|
||||
Let's fix up the fractured IP set from the previous section by re-adding the IP addresses we removed.
|
||||
|
||||
>>> s1.add('255.255.255.255')
|
||||
>>> s1
|
||||
IPSet(['0.0.0.1/32', '0.0.0.2/31', ..., '64.0.0.0/2', '128.0.0.0/1'])
|
||||
|
||||
Getting better.
|
||||
|
||||
>>> list(s1.iter_cidrs())
|
||||
[IPNetwork('0.0.0.1/32'), IPNetwork('0.0.0.2/31'), ..., IPNetwork('64.0.0.0/2'), IPNetwork('128.0.0.0/1')]
|
||||
|
||||
>>> len(list(s1.iter_cidrs()))
|
||||
32
|
||||
|
||||
Add back the other IP address.
|
||||
|
||||
>>> s1.add('0.0.0.0')
|
||||
|
||||
And we're back to our original address.
|
||||
|
||||
>>> s1
|
||||
IPSet(['0.0.0.0/0'])
|
||||
|
||||
----------------------
|
||||
Pickling IPSet objects
|
||||
----------------------
|
||||
|
||||
As with all other netaddr classes, you can use ``pickle`` to persist IP sets for later use.
|
||||
|
||||
>>> import pickle
|
||||
>>> ip_data = IPSet(['10.0.0.0/16', 'fe80::/64'])
|
||||
>>> buf = pickle.dumps(ip_data)
|
||||
>>> ip_data_unpickled = pickle.loads(buf)
|
||||
>>> ip_data == ip_data_unpickled
|
||||
True
|
||||
@@ -0,0 +1,94 @@
|
||||
=Socket Fallback Module Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.fbsocket import *
|
||||
|
||||
}}}
|
||||
|
||||
IPv6 '::' compression algorithm tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:0:0:0:0:0:0:0'))
|
||||
'::'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:0:0:0:0:0:0:A'))
|
||||
'::a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:0:0:0:0:0:0'))
|
||||
'a::'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:A:0:0:0:0:0'))
|
||||
'a:0:a::'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:0:0:0:0:0:A'))
|
||||
'a::a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:A:0:0:0:0:0:A'))
|
||||
'0:a::a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:A:0:0:0:0:A'))
|
||||
'a:0:a::a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:0:0:A:0:0:0:A'))
|
||||
'::a:0:0:0:a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:0:0:0:A:0:0:A'))
|
||||
'::a:0:0:a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:0:0:0:A:0:A'))
|
||||
'a::a:0:a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:0:A:0:0:A:0'))
|
||||
'a::a:0:0:a:0'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:A:0:A:0:A:0'))
|
||||
'a:0:a:0:a:0:a:0'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:A:0:A:0:A:0:A'))
|
||||
'0:a:0:a:0:a:0:a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '1080:0:0:0:8:800:200C:417A'))
|
||||
'1080::8:800:200c:417a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210'))
|
||||
'fedc:ba98:7654:3210:fedc:ba98:7654:3210'
|
||||
|
||||
}}}
|
||||
|
||||
IPv4 failure tests
|
||||
|
||||
{{{
|
||||
|
||||
>>> inet_ntoa(1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: string type expected, not <type 'int'>
|
||||
|
||||
|
||||
>>> inet_ntoa('\x00')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: invalid length of packed IP address string
|
||||
|
||||
>>> inet_aton('0x0')
|
||||
'\x00\x00\x00\x00'
|
||||
|
||||
>>> inet_aton('010')
|
||||
'\x08\x00\x00\x00'
|
||||
|
||||
}}}
|
||||
|
||||
IPv6 failure tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> inet_pton(AF_INET6, '::0x07f')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: illegal IP address string '::0x07f'
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,108 @@
|
||||
=IP Subnet Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Incrementing IP objects.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.0/28')
|
||||
>>> for i in range(16):
|
||||
... str(ip)
|
||||
... ip += 1
|
||||
'192.0.2.0/28'
|
||||
'192.0.2.16/28'
|
||||
'192.0.2.32/28'
|
||||
'192.0.2.48/28'
|
||||
'192.0.2.64/28'
|
||||
'192.0.2.80/28'
|
||||
'192.0.2.96/28'
|
||||
'192.0.2.112/28'
|
||||
'192.0.2.128/28'
|
||||
'192.0.2.144/28'
|
||||
'192.0.2.160/28'
|
||||
'192.0.2.176/28'
|
||||
'192.0.2.192/28'
|
||||
'192.0.2.208/28'
|
||||
'192.0.2.224/28'
|
||||
'192.0.2.240/28'
|
||||
|
||||
>>> ip = IPNetwork('2001:470:1f04::/48')
|
||||
>>> for i in ip.subnet(128):
|
||||
... print i
|
||||
... break
|
||||
2001:470:1f04::/128
|
||||
|
||||
}}}
|
||||
|
||||
IP address and subnet sortability.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip_list = []
|
||||
|
||||
>>> for subnet in IPNetwork('192.0.2.0/24').subnet(28, 3):
|
||||
... ip_list.append(subnet)
|
||||
... ip_list.extend([ip for ip in subnet])
|
||||
|
||||
>>> for addr in sorted(ip_list):
|
||||
... print '%r' % addr
|
||||
IPNetwork('192.0.2.0/28')
|
||||
IPAddress('192.0.2.0')
|
||||
IPAddress('192.0.2.1')
|
||||
IPAddress('192.0.2.2')
|
||||
IPAddress('192.0.2.3')
|
||||
IPAddress('192.0.2.4')
|
||||
IPAddress('192.0.2.5')
|
||||
IPAddress('192.0.2.6')
|
||||
IPAddress('192.0.2.7')
|
||||
IPAddress('192.0.2.8')
|
||||
IPAddress('192.0.2.9')
|
||||
IPAddress('192.0.2.10')
|
||||
IPAddress('192.0.2.11')
|
||||
IPAddress('192.0.2.12')
|
||||
IPAddress('192.0.2.13')
|
||||
IPAddress('192.0.2.14')
|
||||
IPAddress('192.0.2.15')
|
||||
IPNetwork('192.0.2.16/28')
|
||||
IPAddress('192.0.2.16')
|
||||
IPAddress('192.0.2.17')
|
||||
IPAddress('192.0.2.18')
|
||||
IPAddress('192.0.2.19')
|
||||
IPAddress('192.0.2.20')
|
||||
IPAddress('192.0.2.21')
|
||||
IPAddress('192.0.2.22')
|
||||
IPAddress('192.0.2.23')
|
||||
IPAddress('192.0.2.24')
|
||||
IPAddress('192.0.2.25')
|
||||
IPAddress('192.0.2.26')
|
||||
IPAddress('192.0.2.27')
|
||||
IPAddress('192.0.2.28')
|
||||
IPAddress('192.0.2.29')
|
||||
IPAddress('192.0.2.30')
|
||||
IPAddress('192.0.2.31')
|
||||
IPNetwork('192.0.2.32/28')
|
||||
IPAddress('192.0.2.32')
|
||||
IPAddress('192.0.2.33')
|
||||
IPAddress('192.0.2.34')
|
||||
IPAddress('192.0.2.35')
|
||||
IPAddress('192.0.2.36')
|
||||
IPAddress('192.0.2.37')
|
||||
IPAddress('192.0.2.38')
|
||||
IPAddress('192.0.2.39')
|
||||
IPAddress('192.0.2.40')
|
||||
IPAddress('192.0.2.41')
|
||||
IPAddress('192.0.2.42')
|
||||
IPAddress('192.0.2.43')
|
||||
IPAddress('192.0.2.44')
|
||||
IPAddress('192.0.2.45')
|
||||
IPAddress('192.0.2.46')
|
||||
IPAddress('192.0.2.47')
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,733 @@
|
||||
First of all you need to pull the various netaddr classes and functions into your namespace.
|
||||
|
||||
.. note:: Do this for the purpose of this tutorial only. In your own code, you should be explicit about the classes, functions and constants you import to avoid name clashes.
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
We also import the standard library module `pprint` to help format our output.
|
||||
|
||||
>>> import pprint
|
||||
|
||||
----------------
|
||||
Basic operations
|
||||
----------------
|
||||
|
||||
The following `IPAddress` object represents a single IP address.
|
||||
|
||||
>>> ip = IPAddress('192.0.2.1')
|
||||
>>> ip.version
|
||||
4
|
||||
|
||||
The `repr()` call returns a Python statement that can be used to reconstruct an equivalent IP address object state from scratch when run in the Python interpreter.
|
||||
|
||||
>>> repr(ip)
|
||||
"IPAddress('192.0.2.1')"
|
||||
>>> ip
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
Access in the string context returns the IP object as a string value.
|
||||
|
||||
>>> str(ip)
|
||||
'192.0.2.1'
|
||||
>>> '%s' % ip
|
||||
'192.0.2.1'
|
||||
>>> ip.format() # only really useful for IPv6 addresses.
|
||||
'192.0.2.1'
|
||||
|
||||
------------------------
|
||||
Numerical representation
|
||||
------------------------
|
||||
|
||||
You can view an IP address in various other formats.
|
||||
|
||||
>>> int(ip) == 3221225985
|
||||
True
|
||||
>>> hex(ip)
|
||||
'0xc0000201'
|
||||
>>> ip.bin
|
||||
'0b11000000000000000000001000000001'
|
||||
>>> ip.bits()
|
||||
'11000000.00000000.00000010.00000001'
|
||||
>>> ip.words == (192, 0, 2, 1)
|
||||
True
|
||||
|
||||
---------------------------------
|
||||
Representing networks and subnets
|
||||
---------------------------------
|
||||
|
||||
`IPNetwork` objects are used to represent subnets, networks or VLANs that accept CIDR prefixes and netmasks.
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.1')
|
||||
>>> ip.ip
|
||||
IPAddress('192.0.2.1')
|
||||
>>> ip.network, ip.broadcast
|
||||
(IPAddress('192.0.2.1'), IPAddress('192.0.2.1'))
|
||||
>>> ip.netmask, ip.hostmask
|
||||
(IPAddress('255.255.255.255'), IPAddress('0.0.0.0'))
|
||||
>>> ip.size
|
||||
1
|
||||
|
||||
In this case, the network and broadcast address are the same, akin to a host route.
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.0/24')
|
||||
>>> ip.ip
|
||||
IPAddress('192.0.2.0')
|
||||
>>> ip.network, ip.broadcast
|
||||
(IPAddress('192.0.2.0'), IPAddress('192.0.2.255'))
|
||||
>>> ip.netmask, ip.hostmask
|
||||
(IPAddress('255.255.255.0'), IPAddress('0.0.0.255'))
|
||||
>>> ip.size
|
||||
256
|
||||
|
||||
And finally, this IPNetwork object represents an IP address that belongs to a given IP subnet.
|
||||
|
||||
>>> ip = IPNetwork('192.0.3.112/22')
|
||||
>>> ip.ip
|
||||
IPAddress('192.0.3.112')
|
||||
>>> ip.network, ip.broadcast
|
||||
(IPAddress('192.0.0.0'), IPAddress('192.0.3.255'))
|
||||
>>> ip.netmask, ip.hostmask
|
||||
(IPAddress('255.255.252.0'), IPAddress('0.0.3.255'))
|
||||
>>> ip.size
|
||||
1024
|
||||
|
||||
Internally, each IPNetwork object only stores 3 values :-
|
||||
|
||||
* the IP address value as an unsigned integer
|
||||
* a reference to the IP protocol module for the IP version being represented
|
||||
* the CIDR prefix bitmask
|
||||
|
||||
All the other values are calculated on-the-fly on access.
|
||||
|
||||
It is possible to adjust the IP address value and the CIDR prefix after object instantiation.
|
||||
|
||||
>>> ip = IPNetwork('0.0.0.0/0')
|
||||
>>> ip
|
||||
IPNetwork('0.0.0.0/0')
|
||||
>>> ip.value = 3221225985
|
||||
>>> ip
|
||||
IPNetwork('192.0.2.1/0')
|
||||
>>> ip.prefixlen
|
||||
0
|
||||
>>> ip.prefixlen = 23
|
||||
>>> ip
|
||||
IPNetwork('192.0.2.1/23')
|
||||
|
||||
There is also a property that lets you access the *true* CIDR address which removes all host bits from the network address based on the CIDR subnet prefix.
|
||||
|
||||
>>> ip.cidr
|
||||
IPNetwork('192.0.2.0/23')
|
||||
|
||||
This is handy for specifying some networking configurations correctly.
|
||||
|
||||
If you want to access information about each of the various IP addresses that form the IP subnet, this is available by performing pass through calls to sub methods of each `IPAddress` object.
|
||||
|
||||
For example if you want to see a binary digit representation of each address you can do the following.
|
||||
|
||||
>>> ip.ip.bits()
|
||||
'11000000.00000000.00000010.00000001'
|
||||
>>> ip.network.bits()
|
||||
'11000000.00000000.00000010.00000000'
|
||||
>>> ip.netmask.bits()
|
||||
'11111111.11111111.11111110.00000000'
|
||||
>>> ip.broadcast.bits()
|
||||
'11000000.00000000.00000011.11111111'
|
||||
|
||||
------------
|
||||
IPv6 support
|
||||
------------
|
||||
|
||||
Full support for IPv6 is provided. Let's try a few examples:
|
||||
|
||||
>>> ip = IPAddress(0, 6)
|
||||
>>> ip
|
||||
IPAddress('::')
|
||||
>>> ip = IPNetwork('fe80::dead:beef/64')
|
||||
>>> str(ip), ip.prefixlen, ip.version
|
||||
('fe80::dead:beef/64', 64, 6)
|
||||
>>> int(ip.ip) == 338288524927261089654018896845083623151
|
||||
True
|
||||
>>> hex(ip.ip)
|
||||
'0xfe8000000000000000000000deadbeef'
|
||||
|
||||
Bit-style output isn't as quite as friendly as hexadecimal for such a long numbers, but here the proof that it works!
|
||||
|
||||
>>> ip.ip.bits()
|
||||
'1111111010000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1101111010101101:1011111011101111'
|
||||
|
||||
Here are some networking details for an IPv6 subnet.
|
||||
|
||||
>>> ip.network, ip.broadcast, ip.netmask, ip.hostmask
|
||||
(IPAddress('fe80::'), IPAddress('fe80::ffff:ffff:ffff:ffff'), IPAddress('ffff:ffff:ffff:ffff::'), IPAddress('::ffff:ffff:ffff:ffff'))
|
||||
|
||||
--------------------------------------
|
||||
Interoperability between IPv4 and IPv6
|
||||
--------------------------------------
|
||||
|
||||
It is likely that with IPv6 becoming more prevalent, you'll want to be able to interoperate between IPv4 and IPv6 address seemlessly.
|
||||
|
||||
Here are a couple of methods that help achieve this.
|
||||
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
IPv4 to IPv6 conversion
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
>>> IPAddress('192.0.2.15').ipv4()
|
||||
IPAddress('192.0.2.15')
|
||||
>>> IPAddress('192.0.2.15').ipv6()
|
||||
IPAddress('::ffff:192.0.2.15')
|
||||
>>> IPAddress('192.0.2.15').ipv6(ipv4_compatible=True)
|
||||
IPAddress('::192.0.2.15')
|
||||
>>> IPAddress('192.0.2.15').ipv6(True)
|
||||
IPAddress('::192.0.2.15')
|
||||
>>> ip = IPNetwork('192.0.2.1/23')
|
||||
>>> ip.ipv4()
|
||||
IPNetwork('192.0.2.1/23')
|
||||
>>> ip.ipv6()
|
||||
IPNetwork('::ffff:192.0.2.1/119')
|
||||
>>> ip.ipv6(ipv4_compatible=True)
|
||||
IPNetwork('::192.0.2.1/119')
|
||||
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
IPv6 to IPv4 conversion
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
>>> IPNetwork('::ffff:192.0.2.1/119').ipv6()
|
||||
IPNetwork('::ffff:192.0.2.1/119')
|
||||
>>> IPNetwork('::ffff:192.0.2.1/119').ipv6(ipv4_compatible=True)
|
||||
IPNetwork('::192.0.2.1/119')
|
||||
>>> IPNetwork('::ffff:192.0.2.1/119').ipv4()
|
||||
IPNetwork('192.0.2.1/23')
|
||||
>>> IPNetwork('::192.0.2.1/119').ipv4()
|
||||
IPNetwork('192.0.2.1/23')
|
||||
|
||||
Note that the IP object returns IPv4 "mapped" addresses by default in preference to IPv4 "compatible" ones. This has been chosen purposefully as the latter form has been deprecated (see RFC 4291 for details).
|
||||
|
||||
---------------
|
||||
List operations
|
||||
---------------
|
||||
|
||||
If you treat an `IPNetwork` object as if it were a standard Python list object it will give you access to a list of individual IP address objects. This of course is illusory and they are not created until you access them.
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.16/29')
|
||||
|
||||
Accessing an IP object using the `list()` context invokes the default generator which returns a list of all IP objects in the range specified by the IP object's subnet.
|
||||
|
||||
>>> ip_list = list(ip)
|
||||
>>> len(ip_list)
|
||||
8
|
||||
>>> ip_list
|
||||
[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), ..., IPAddress('192.0.2.22'), IPAddress('192.0.2.23')]
|
||||
|
||||
The length of that list is 8 individual IP addresses.
|
||||
|
||||
>>> len(ip)
|
||||
8
|
||||
|
||||
^^^^^^^^
|
||||
Indexing
|
||||
^^^^^^^^
|
||||
|
||||
You can use standard index access to IP addresses in the subnet.
|
||||
|
||||
>>> ip[0]
|
||||
IPAddress('192.0.2.16')
|
||||
>>> ip[1]
|
||||
IPAddress('192.0.2.17')
|
||||
>>> ip[-1]
|
||||
IPAddress('192.0.2.23')
|
||||
|
||||
^^^^^^^
|
||||
Slicing
|
||||
^^^^^^^
|
||||
|
||||
You can also use list slices on IP addresses in the subnet.
|
||||
|
||||
>>> ip[0:4]
|
||||
<generator object ...>
|
||||
|
||||
The slice is a generator function. This was done to save time and system resources as some slices can end up being very large for certain subnets!
|
||||
|
||||
Here is how you'd access all elements in a slice.
|
||||
|
||||
>>> list(ip[0:4])
|
||||
[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), IPAddress('192.0.2.18'), IPAddress('192.0.2.19')]
|
||||
|
||||
Extended slicing is also supported.
|
||||
|
||||
>>> list(ip[0::2])
|
||||
[IPAddress('192.0.2.16'), IPAddress('192.0.2.18'), IPAddress('192.0.2.20'), IPAddress('192.0.2.22')]
|
||||
|
||||
List reversal.
|
||||
|
||||
>>> list(ip[-1::-1])
|
||||
[IPAddress('192.0.2.23'), IPAddress('192.0.2.22'), ..., IPAddress('192.0.2.17'), IPAddress('192.0.2.16')]
|
||||
|
||||
Use of generators ensures working with large IP subnets is efficient.
|
||||
|
||||
>>> for ip in IPNetwork('192.0.2.0/23'):
|
||||
... print '%s' % ip
|
||||
...
|
||||
192.0.2.0
|
||||
192.0.2.1
|
||||
192.0.2.2
|
||||
192.0.2.3
|
||||
...
|
||||
192.0.3.252
|
||||
192.0.3.253
|
||||
192.0.3.254
|
||||
192.0.3.255
|
||||
|
||||
In IPv4 networks you only usually assign the addresses between the network and broadcast addresses to actual host interfaces on systems.
|
||||
|
||||
Here is the iterator provided for accessing these IP addresses :-
|
||||
|
||||
>>> for ip in IPNetwork('192.0.2.0/23').iter_hosts():
|
||||
... print '%s' % ip
|
||||
...
|
||||
192.0.2.1
|
||||
192.0.2.2
|
||||
192.0.2.3
|
||||
192.0.2.4
|
||||
...
|
||||
192.0.3.251
|
||||
192.0.3.252
|
||||
192.0.3.253
|
||||
192.0.3.254
|
||||
|
||||
---------------------------------
|
||||
Sorting IP addresses and networks
|
||||
---------------------------------
|
||||
|
||||
It is fairly common and useful to be able to sort IP addresses and networks canonically.
|
||||
|
||||
Here is how sorting works with individual addresses.
|
||||
|
||||
>>> import random
|
||||
>>> ip_list = list(IPNetwork('192.0.2.128/28'))
|
||||
>>> random.shuffle(ip_list)
|
||||
>>> sorted(ip_list)
|
||||
[IPAddress('192.0.2.128'), IPAddress('192.0.2.129'), ..., IPAddress('192.0.2.142'), IPAddress('192.0.2.143')]
|
||||
|
||||
For convenience, you are able to sort IP subnets at the same time as addresses and they can be combinations of IPv4 and IPv6 addresses at the same time as well (IPv4 addresses and network appear before IPv6 ones).
|
||||
|
||||
>>> ip_list = [
|
||||
... IPAddress('192.0.2.130'),
|
||||
... IPAddress('10.0.0.1'),
|
||||
... IPNetwork('192.0.2.128/28'),
|
||||
... IPNetwork('192.0.3.0/24'),
|
||||
... IPNetwork('192.0.2.0/24'),
|
||||
... IPNetwork('fe80::/64'),
|
||||
... IPAddress('::'),
|
||||
... IPNetwork('172.24/12')]
|
||||
>>> random.shuffle(ip_list)
|
||||
>>> ip_list.sort()
|
||||
>>> pprint.pprint(ip_list)
|
||||
[IPAddress('10.0.0.1'),
|
||||
IPNetwork('172.24.0.0/12'),
|
||||
IPNetwork('192.0.2.0/24'),
|
||||
IPNetwork('192.0.2.128/28'),
|
||||
IPAddress('192.0.2.130'),
|
||||
IPNetwork('192.0.3.0/24'),
|
||||
IPAddress('::'),
|
||||
IPNetwork('fe80::/64')]
|
||||
|
||||
Notice how overlapping subnets also sort in order from largest to smallest.
|
||||
|
||||
-----------------------------------------
|
||||
Summarizing list of addresses and subnets
|
||||
-----------------------------------------
|
||||
|
||||
Another useful operation is the ability to summarize groups of IP subnets and addresses, merging them together where possible to create the smallest possible list of CIDR subnets.
|
||||
|
||||
You do this in netaddr using the `cidr_merge()` function.
|
||||
|
||||
First we create a list of IP objects that contains a good mix of individual addresses and subnets, along with some string based IP address values for good measure. To make things more interesting some IPv6 addresses are thrown in as well.
|
||||
|
||||
>>> ip_list = [ip for ip in IPNetwork('fe80::/120')]
|
||||
>>> ip_list.append(IPNetwork('192.0.2.0/24'))
|
||||
>>> ip_list.extend([str(ip) for ip in IPNetwork('192.0.3.0/24')])
|
||||
>>> ip_list.append(IPNetwork('192.0.4.0/25'))
|
||||
>>> ip_list.append(IPNetwork('192.0.4.128/25'))
|
||||
>>> len(ip_list)
|
||||
515
|
||||
>>> cidr_merge(ip_list)
|
||||
[IPNetwork('192.0.2.0/23'), IPNetwork('192.0.4.0/24'), IPNetwork('fe80::/120')]
|
||||
|
||||
Useful isn't it?
|
||||
|
||||
---------------------
|
||||
Supernets and subnets
|
||||
---------------------
|
||||
|
||||
It is quite common to have a large CIDR subnet that you may want to split up into multiple smaller component blocks to better manage your network allocations, firewall rules etcc and netaddr gives you the tools required to do this.
|
||||
|
||||
Here we take a large /16 private class B network block and split it up into a set of smaller 512 sized blocks.
|
||||
|
||||
>>> ip = IPNetwork('172.24.0.0/16')
|
||||
>>> ip.subnet(23)
|
||||
<generator object ...>
|
||||
|
||||
Once again, this method produces and iterator because of the possibility for a large number of return values depending on this subnet size specified.
|
||||
|
||||
>>> subnets = list(ip.subnet(23))
|
||||
>>> len(subnets)
|
||||
128
|
||||
>>> subnets
|
||||
[IPNetwork('172.24.0.0/23'), IPNetwork('172.24.2.0/23'), IPNetwork('172.24.4.0/23'), ..., IPNetwork('172.24.250.0/23'), IPNetwork('172.24.252.0/23'), IPNetwork('172.24.254.0/23')]
|
||||
|
||||
It is also possible to retrieve the list of supernets that a given IP address or subnet belongs to. You can also specify an optional limit.
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.114')
|
||||
>>> supernets = ip.supernet(22)
|
||||
>>> pprint.pprint(supernets)
|
||||
[IPNetwork('192.0.0.0/22'),
|
||||
IPNetwork('192.0.2.0/23'),
|
||||
IPNetwork('192.0.2.0/24'),
|
||||
IPNetwork('192.0.2.0/25'),
|
||||
IPNetwork('192.0.2.64/26'),
|
||||
IPNetwork('192.0.2.96/27'),
|
||||
IPNetwork('192.0.2.112/28'),
|
||||
IPNetwork('192.0.2.112/29'),
|
||||
IPNetwork('192.0.2.112/30'),
|
||||
IPNetwork('192.0.2.114/31')]
|
||||
|
||||
Here, we return a list rather than a generator because the potential list of values is of a predictable size (no more than 31 subnets for an IPv4 address and 127 for IPv6).
|
||||
|
||||
---------------------------------------
|
||||
Support for non-standard address ranges
|
||||
---------------------------------------
|
||||
|
||||
While CIDR is a useful way to describe networks succinctly, it is often necessary (particularly with IPv4 which predates the CIDR specification) to be able to generate lists of IP addresses that have an arbitrary start and end address that do not fall on strict bit mask boundaries.
|
||||
|
||||
The `iter_iprange()` function allow you to do just this.
|
||||
|
||||
>>> ip_list = list(iter_iprange('192.0.2.1', '192.0.2.14'))
|
||||
>>> len(ip_list)
|
||||
14
|
||||
>>> ip_list
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), ..., IPAddress('192.0.2.13'), IPAddress('192.0.2.14')]
|
||||
|
||||
It is equally nice to know what the actual list of CIDR subnets is that would correctly cover this non-aligned range of addresses.
|
||||
|
||||
Here `cidr_merge()` comes to the rescue once more.
|
||||
|
||||
>>> cidr_merge(ip_list)
|
||||
[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/30'), IPNetwork('192.0.2.12/31'), IPNetwork('192.0.2.14/32')]
|
||||
|
||||
--------------------------------------------
|
||||
Dealing with older IP network specifications
|
||||
--------------------------------------------
|
||||
|
||||
Until the advent of the CIDR specification it was common to infer the netmask of an IPv4 address based on its first octet using an set of classful rules (first defined in RFC 791).
|
||||
|
||||
You frequently come across reference to them in various RFCs and they are well supported by a number of software libraries. For completeness, rather than leave out this important (but now somewhat historical) set of rules, they are supported via the cryptically named `cidr_abbrev_to_verbose()` function.
|
||||
|
||||
Here is an example of these rules for the whole of the IPv4 address space.
|
||||
|
||||
>>> cidrs = [cidr_abbrev_to_verbose(octet) for octet in range(0, 256)]
|
||||
>>> pprint.pprint(cidrs)
|
||||
['0.0.0.0/8',
|
||||
...
|
||||
'127.0.0.0/8',
|
||||
'128.0.0.0/16',
|
||||
...
|
||||
'191.0.0.0/16',
|
||||
'192.0.0.0/24',
|
||||
...
|
||||
'223.0.0.0/24',
|
||||
'224.0.0.0/4',
|
||||
...
|
||||
'239.0.0.0/4',
|
||||
'240.0.0.0/32',
|
||||
...
|
||||
'255.0.0.0/32']
|
||||
>>> len(cidrs)
|
||||
256
|
||||
|
||||
-------------------------
|
||||
IP address categorisation
|
||||
-------------------------
|
||||
|
||||
IP addresses fall into several categories, not all of which are suitable for assignment as host addresses.
|
||||
|
||||
^^^^^^^
|
||||
Unicast
|
||||
^^^^^^^
|
||||
|
||||
>>> IPAddress('192.0.2.1').is_unicast()
|
||||
True
|
||||
>>> IPAddress('fe80::1').is_unicast()
|
||||
True
|
||||
|
||||
^^^^^^^^^
|
||||
Multicast
|
||||
^^^^^^^^^
|
||||
|
||||
Used to indentify multicast groups (see RFC 2365 and 3171 for more info).
|
||||
|
||||
>>> IPAddress('239.192.0.1').is_multicast()
|
||||
True
|
||||
>>> IPAddress('ff00::1').is_multicast()
|
||||
True
|
||||
|
||||
^^^^^^^
|
||||
Private
|
||||
^^^^^^^
|
||||
|
||||
Found on intranets and used behind NAT routers.
|
||||
|
||||
>>> IPAddress('172.24.0.1').is_private()
|
||||
True
|
||||
>>> IPAddress('10.0.0.1').is_private()
|
||||
True
|
||||
>>> IPAddress('192.168.0.1').is_private()
|
||||
True
|
||||
|
||||
^^^^^^^^
|
||||
Reserved
|
||||
^^^^^^^^
|
||||
|
||||
Addresses in reserved ranges are not available for general use.
|
||||
|
||||
>>> IPAddress('253.0.0.1').is_reserved()
|
||||
True
|
||||
|
||||
^^^^^^
|
||||
Public
|
||||
^^^^^^
|
||||
|
||||
Addresses accessible via the Internet.
|
||||
|
||||
.. note:: circa the end of 2011 all IPv4 addresses had been allocated to the Regional Internet Registrars. A booming after market in IPv4 addresses has started. There is still plenty of life left in this protocol version yet :)
|
||||
|
||||
>>> ip = IPAddress('62.125.24.5')
|
||||
>>> ip.is_unicast() and not ip.is_private()
|
||||
True
|
||||
|
||||
^^^^^^^^
|
||||
Netmasks
|
||||
^^^^^^^^
|
||||
|
||||
A bitmask used to divide an IP address into its network address and host address.
|
||||
|
||||
>>> IPAddress('255.255.254.0').is_netmask()
|
||||
True
|
||||
|
||||
^^^^^^^^^
|
||||
Hostmasks
|
||||
^^^^^^^^^
|
||||
|
||||
Similar to a netmask but with the all the bits flipped the opposite way.
|
||||
|
||||
>>> IPAddress('0.0.1.255').is_hostmask()
|
||||
True
|
||||
|
||||
^^^^^^^^
|
||||
Loopback
|
||||
^^^^^^^^
|
||||
|
||||
These addresses are used internally within an IP network stack and packets sent to these addresses are not distributed via a physical network connection.
|
||||
|
||||
>>> IPAddress('127.0.0.1').is_loopback()
|
||||
True
|
||||
>>> IPAddress('::1').is_loopback()
|
||||
True
|
||||
|
||||
----------------------
|
||||
Comparing IP addresses
|
||||
----------------------
|
||||
|
||||
`IPAddress` objects can be compared with each other. As an `IPAddress` object can represent both an individual IP address and an implicit network, it pays to get both sides of your comparison into the same terms before you compare them to avoid odd results.
|
||||
|
||||
Here are some comparisons of individual IP address to get the ball rolling.
|
||||
|
||||
>>> IPAddress('192.0.2.1') == IPAddress('192.0.2.1')
|
||||
True
|
||||
>>> IPAddress('192.0.2.1') < IPAddress('192.0.2.2')
|
||||
True
|
||||
>>> IPAddress('192.0.2.2') > IPAddress('192.0.2.1')
|
||||
True
|
||||
>>> IPAddress('192.0.2.1') != IPAddress('192.0.2.1')
|
||||
False
|
||||
>>> IPAddress('192.0.2.1') >= IPAddress('192.0.2.1')
|
||||
True
|
||||
>>> IPAddress('192.0.2.2') >= IPAddress('192.0.2.1')
|
||||
True
|
||||
>>> IPAddress('192.0.2.1') <= IPAddress('192.0.2.1')
|
||||
True
|
||||
>>> IPAddress('192.0.2.1') <= IPAddress('192.0.2.2')
|
||||
True
|
||||
|
||||
Now, lets try something a little more interesting.
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') == IPNetwork('192.0.2.112/24')
|
||||
True
|
||||
|
||||
Hmmmmmmmm... looks a bit odd doesn't it? That's because by default, IP objects compare their subnets (or lower and upper boundaries) rather than their individual IP address values.
|
||||
|
||||
The solution to this situation is very simple. Knowing this default behaviour, just be explicit about exactly which portion of each IP object you'd like to compare using pass-through properties.
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24').ip == IPNetwork('192.0.2.112/24').ip
|
||||
False
|
||||
>>> IPNetwork('192.0.2.0/24').ip < IPNetwork('192.0.2.112/24').ip
|
||||
True
|
||||
|
||||
That's more like it. You can also be explicit about comparing networks in this way if you so wish (although it is not strictly necessary).
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24').cidr == IPNetwork('192.0.2.112/24').cidr
|
||||
True
|
||||
|
||||
Armed with this information here are some examples of network comparisons.
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') == IPNetwork('192.0.3.0/24')
|
||||
False
|
||||
>>> IPNetwork('192.0.2.0/24') < IPNetwork('192.0.3.0/24')
|
||||
True
|
||||
>>> IPNetwork('192.0.2.0/24') < IPNetwork('192.0.3.0/24')
|
||||
True
|
||||
|
||||
This will inevitably raise questions about comparing IPAddress (scalar) objects and IPNetwork (vector) objects with each other (or at least it should).
|
||||
|
||||
Here is how netaddr chooses to address this situation.
|
||||
|
||||
>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32')
|
||||
False
|
||||
>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32')
|
||||
True
|
||||
|
||||
An IP network or subnet is different from an individual IP address and therefore cannot be (directly) compared.
|
||||
|
||||
If you want to compare them successfully, you must be explicit about which aspect of the IP network you wish to match against the IP address in question.
|
||||
|
||||
You can use the index of the first or last address if it is a /32 like so :-
|
||||
|
||||
>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32')[0]
|
||||
True
|
||||
>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32')[-1]
|
||||
True
|
||||
>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32')[0]
|
||||
False
|
||||
|
||||
You can also use the base address if this is what you wish to compare :-
|
||||
|
||||
>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32').ip
|
||||
True
|
||||
>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32').ip
|
||||
False
|
||||
|
||||
While this may seem a bit pointless at first, netaddr strives to keep IP addresses and network separate from one another while still allowing reasonable interoperability.
|
||||
|
||||
-----------
|
||||
DNS support
|
||||
-----------
|
||||
|
||||
It is a common administrative task to generate reverse IP lookups for DNS. This is particularly arduous for IPv6 addresses.
|
||||
|
||||
Here is how you do this using an IPAddress object's `reverse_dns()` method.
|
||||
|
||||
>>> IPAddress('172.24.0.13').reverse_dns
|
||||
'13.0.24.172.in-addr.arpa.'
|
||||
>>> IPAddress('fe80::feeb:daed').reverse_dns
|
||||
'd.e.a.d.b.e.e.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa.'
|
||||
|
||||
Note that ``ip6.int`` is not used as this has been deprecated (see RFC 3152 for details).
|
||||
|
||||
---------------------------
|
||||
Non standard address ranges
|
||||
---------------------------
|
||||
|
||||
As CIDR is a relative newcomer given the long history of IP version 4 you are quite likely to come across systems and documentation which make reference to IP address ranges in formats other than CIDR. Converting from these arbitrary range types to CIDR and back again isn't a particularly fun task. Fortunately, netaddr tries to make this job easy for you with two purpose built classes.
|
||||
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
Arbitrary IP address ranges
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
You can represent an arbitrary IP address range using a lower and upper bound address in the form of an IPRange object.
|
||||
|
||||
>>> r1 = IPRange('192.0.2.1', '192.0.2.15')
|
||||
>>> r1
|
||||
IPRange('192.0.2.1', '192.0.2.15')
|
||||
|
||||
You can iterate across and index these ranges just like and IPNetwork object.
|
||||
|
||||
Importantly, you can also convert it to it's CIDR equivalent.
|
||||
|
||||
>>> r1.cidrs()
|
||||
[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')]
|
||||
|
||||
Here is how individual IPRange and IPNetwork compare.
|
||||
|
||||
>>> IPRange('192.0.2.0', '192.0.2.255') != IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
>>> IPRange('192.0.2.0', '192.0.2.255') == IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
You may wish to compare an IP range against a list of IPAddress and IPNetwork
|
||||
objects.
|
||||
|
||||
>>> r1 = IPRange('192.0.2.1', '192.0.2.15')
|
||||
>>> addrs = list(r1)
|
||||
>>> addrs
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15')]
|
||||
>>> r1 == addrs
|
||||
False
|
||||
|
||||
Oops! Not quite what we were looking for or expecting.
|
||||
|
||||
The way to do this is to get either side of the comparison operation into the same terms.
|
||||
|
||||
>>> list(r1) == addrs
|
||||
True
|
||||
|
||||
That's more like it.
|
||||
|
||||
The same goes for IPNetwork objects.
|
||||
|
||||
>>> subnets = r1.cidrs()
|
||||
>>> subnets
|
||||
[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')]
|
||||
>>> r1 == subnets
|
||||
False
|
||||
>>> r1.cidrs() == subnets
|
||||
True
|
||||
|
||||
The above works if the list you are comparing contains one type or the other, but what if you have a mixed list of `IPAddress`, `IPNetwork` and string addresses?
|
||||
|
||||
Time for some slightly more powerful operations. Let's make use of a new class for dealing with groups of IP addresses and subnets. The IPSet class.
|
||||
|
||||
>>> ips = [IPAddress('192.0.2.1'), '192.0.2.2/31', IPNetwork('192.0.2.4/31'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), '192.0.2.8', '192.0.2.9', IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPNetwork('192.0.2.12/30')]
|
||||
>>> s1 = IPSet(r1.cidrs())
|
||||
>>> s2 = IPSet(ips)
|
||||
>>> s2
|
||||
IPSet(['192.0.2.1/32', '192.0.2.2/31', '192.0.2.4/30', '192.0.2.8/29'])
|
||||
>>> s1 == s2
|
||||
True
|
||||
|
||||
Let's remove one of the element from one of the IPSet objects and see what happens.
|
||||
|
||||
>>> s2.pop()
|
||||
IPNetwork('192.0.2.4/30')
|
||||
>>> s1 == s2
|
||||
False
|
||||
|
||||
This is perhaps a somewhat contrived example but it just shows you some of the capabilities on offer.
|
||||
|
||||
See the IPSet tutorial :doc:`tutorial_03` for more details on that class.
|
||||
|
||||
^^^^^^^^^^^^^^
|
||||
IP Glob ranges
|
||||
^^^^^^^^^^^^^^
|
||||
|
||||
netaddr also supports a user friendly form of specifying IP address ranges using a "glob" style syntax.
|
||||
|
||||
.. note:: At present only IPv4 globs are supported.
|
||||
|
||||
>>> IPGlob('192.0.2.*') == IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
IPGlob('192.0.2.*') != IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
As `IPGlob` is a subclass of `IPRange`, all of the same operations apply.
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
=IEEE EUI-48 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy.eui48 import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Smoke Tests==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '00000000-00001111-00011111-00010010-11100111-00110011'
|
||||
>>> i = 64945841971
|
||||
>>> t = (0x0, 0x0f, 0x1f, 0x12, 0xe7, 0x33)
|
||||
>>> s = '00-0F-1F-12-E7-33'
|
||||
>>> p = '\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> bits_to_int(b) == 64945841971
|
||||
True
|
||||
|
||||
>>> int_to_bits(i) == b
|
||||
True
|
||||
|
||||
>>> int_to_str(i)
|
||||
'00-0F-1F-12-E7-33'
|
||||
|
||||
>>> int_to_words(i)
|
||||
(0, 15, 31, 18, 231, 51)
|
||||
|
||||
>>> int_to_packed(i)
|
||||
'\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> str_to_int(s) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(t) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t)) == 64945841971
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 64945841971
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
==Smoke Tests With Alternate Dialects==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '00000000:00001111:00011111:00010010:11100111:00110011'
|
||||
>>> i = 64945841971
|
||||
>>> t = (0x0, 0x0f, 0x1f, 0x12, 0xe7, 0x33)
|
||||
>>> s = '0:f:1f:12:e7:33'
|
||||
>>> p = '\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> bits_to_int(b, mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> int_to_bits(i, mac_unix) == b
|
||||
True
|
||||
|
||||
>>> int_to_str(i, mac_unix)
|
||||
'0:f:1f:12:e7:33'
|
||||
|
||||
>>> int_to_str(i, mac_cisco)
|
||||
'000f.1f12.e733'
|
||||
|
||||
>>> int_to_str(i, mac_unix)
|
||||
'0:f:1f:12:e7:33'
|
||||
|
||||
>>> int_to_words(i, mac_unix)
|
||||
(0, 15, 31, 18, 231, 51)
|
||||
|
||||
>>> int_to_packed(i)
|
||||
'\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> str_to_int(s) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(t, mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t), mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 64945841971
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,130 @@
|
||||
=IP version 4 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
Uses TEST-NET references throughout, as described in RFC 3330.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy.ipv4 import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Smoke Tests==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '11000000.00000000.00000010.00000001'
|
||||
>>> i = 3221225985
|
||||
>>> t = (192, 0, 2, 1)
|
||||
>>> s = '192.0.2.1'
|
||||
>>> p = '\xc0\x00\x02\x01'
|
||||
>>> bin_val = '0b11000000000000000000001000000001'
|
||||
|
||||
>>> bits_to_int(b) == 3221225985
|
||||
True
|
||||
|
||||
>>> int_to_bits(i)
|
||||
'11000000.00000000.00000010.00000001'
|
||||
|
||||
>>> int_to_str(i)
|
||||
'192.0.2.1'
|
||||
|
||||
>>> int_to_words(i) == (192, 0, 2, 1)
|
||||
True
|
||||
|
||||
>>> int_to_packed(i)
|
||||
'\xc0\x00\x02\x01'
|
||||
|
||||
>>> int_to_bin(i)
|
||||
'0b11000000000000000000001000000001'
|
||||
|
||||
>>> int_to_bin(i)
|
||||
'0b11000000000000000000001000000001'
|
||||
|
||||
>>> bin_to_int(bin_val) == 3221225985
|
||||
True
|
||||
|
||||
>>> words_to_int(t) == 3221225985
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t)) == 3221225985
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 3221225985
|
||||
True
|
||||
|
||||
>>> valid_bin(bin_val)
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
== inet_aton() Behavioural Tests ==
|
||||
|
||||
inet_aton() is a very old system call and is very permissive with regard to what is assume is a valid IPv4 address. Unfortunately, it is also the most widely used by system software used in software today, so netaddr supports this behaviour by default.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str_to_int('127') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('0x7f') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('0177') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('127.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('0x7f.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('0177.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('127.0.0.1') == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
== inet_pton() Behavioural Tests ==
|
||||
|
||||
inet_pton() is a newer system call that supports both IPv4 and IPv6. It is a lot more strict about what it deems to be a valid IPv4 address and doesn't support many of the features found in inet_aton() such as support for non- decimal octets, partial numbers of octets, etc.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str_to_int('127', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '127' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0x7f', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '0x7f' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0177', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '0177' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('127.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '127.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0x7f.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '0x7f.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0177.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '0177.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('127.0.0.1', flags=INET_PTON) == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,290 @@
|
||||
=IP version 6 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy.ipv6 import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Smoke Tests==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1111111111111111:1111111111111110'
|
||||
>>> i = 4294967294
|
||||
>>> t = (0, 0, 0, 0, 0, 0, 0xffff, 0xfffe)
|
||||
>>> s = '::255.255.255.254'
|
||||
>>> p = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xfe'
|
||||
|
||||
>>> bits_to_int(b) == 4294967294
|
||||
True
|
||||
|
||||
>>> int_to_bits(i) == b
|
||||
True
|
||||
|
||||
>>> int_to_str(i)
|
||||
'::255.255.255.254'
|
||||
|
||||
>>> int_to_words(i)
|
||||
(0, 0, 0, 0, 0, 0, 65535, 65534)
|
||||
|
||||
>>> int_to_packed(i)
|
||||
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xfe'
|
||||
|
||||
>>> str_to_int(s) == 4294967294
|
||||
True
|
||||
|
||||
>>> words_to_int(t) == 4294967294
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t)) == 4294967294
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 4294967294
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
==More Specific IPv6 Tests==
|
||||
|
||||
IPv6 string address variants that are all equivalent.
|
||||
|
||||
{{{
|
||||
|
||||
>>> i = 42540766411282592856903984951992014763
|
||||
>>> str_to_int('2001:0db8:0000:0000:0000:0000:1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0db8:0000:0000:0000::1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0db8:0:0:0:0:1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0db8:0:0::1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0db8::1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0DB8:0000:0000:0000:0000:1428:57AB') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:DB8::1428:57AB') == i
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Intensive IPv6 string address validation testing.
|
||||
|
||||
Positive tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> valid_addrs = (
|
||||
... # RFC 4291
|
||||
... # Long forms.
|
||||
... 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
|
||||
... '1080:0:0:0:8:800:200C:417A', # a unicast address
|
||||
... 'FF01:0:0:0:0:0:0:43', # a multicast address
|
||||
... '0:0:0:0:0:0:0:1', # the loopback address
|
||||
... '0:0:0:0:0:0:0:0', # the unspecified addresses
|
||||
...
|
||||
... # Short forms.
|
||||
... '1080::8:800:200C:417A', # a unicast address
|
||||
... 'FF01::43', # a multicast address
|
||||
... '::1', # the loopback address
|
||||
... '::', # the unspecified addresses
|
||||
...
|
||||
... # IPv4 compatible forms.
|
||||
... '::192.0.2.1',
|
||||
... '::ffff:192.0.2.1',
|
||||
... '0:0:0:0:0:0:192.0.2.1',
|
||||
... '0:0:0:0:0:FFFF:192.0.2.1',
|
||||
... '0:0:0:0:0:0:13.1.68.3',
|
||||
... '0:0:0:0:0:FFFF:129.144.52.38',
|
||||
... '::13.1.68.3',
|
||||
... '::FFFF:129.144.52.38',
|
||||
...
|
||||
... # Other tests.
|
||||
... '1::',
|
||||
... '::ffff',
|
||||
... 'ffff::',
|
||||
... 'ffff::ffff',
|
||||
... '0:1:2:3:4:5:6:7',
|
||||
... '8:9:a:b:c:d:e:f',
|
||||
... '0:0:0:0:0:0:0:0',
|
||||
... 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
|
||||
... )
|
||||
|
||||
>>> for addr in valid_addrs:
|
||||
... addr, valid_str(addr)
|
||||
('FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', True)
|
||||
('1080:0:0:0:8:800:200C:417A', True)
|
||||
('FF01:0:0:0:0:0:0:43', True)
|
||||
('0:0:0:0:0:0:0:1', True)
|
||||
('0:0:0:0:0:0:0:0', True)
|
||||
('1080::8:800:200C:417A', True)
|
||||
('FF01::43', True)
|
||||
('::1', True)
|
||||
('::', True)
|
||||
('::192.0.2.1', True)
|
||||
('::ffff:192.0.2.1', True)
|
||||
('0:0:0:0:0:0:192.0.2.1', True)
|
||||
('0:0:0:0:0:FFFF:192.0.2.1', True)
|
||||
('0:0:0:0:0:0:13.1.68.3', True)
|
||||
('0:0:0:0:0:FFFF:129.144.52.38', True)
|
||||
('::13.1.68.3', True)
|
||||
('::FFFF:129.144.52.38', True)
|
||||
('1::', True)
|
||||
('::ffff', True)
|
||||
('ffff::', True)
|
||||
('ffff::ffff', True)
|
||||
('0:1:2:3:4:5:6:7', True)
|
||||
('8:9:a:b:c:d:e:f', True)
|
||||
('0:0:0:0:0:0:0:0', True)
|
||||
('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', True)
|
||||
|
||||
}}}
|
||||
|
||||
Negative tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> invalid_addrs = (
|
||||
... 'g:h:i:j:k:l:m:n', # bad chars.
|
||||
... '0:0:0:0:0:0:0:0:0' # too long,
|
||||
... '', # empty string
|
||||
... # Unexpected types.
|
||||
... [],
|
||||
... (),
|
||||
... {},
|
||||
... True,
|
||||
... False,
|
||||
... )
|
||||
|
||||
>>> for addr in invalid_addrs:
|
||||
... addr, valid_str(addr)
|
||||
('g:h:i:j:k:l:m:n', False)
|
||||
('0:0:0:0:0:0:0:0:0', False)
|
||||
([], False)
|
||||
((), False)
|
||||
({}, False)
|
||||
(True, False)
|
||||
(False, False)
|
||||
|
||||
}}}
|
||||
|
||||
String compaction tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> valid_addrs = {
|
||||
... # RFC 4291
|
||||
... 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' : 'fedc:ba98:7654:3210:fedc:ba98:7654:3210',
|
||||
... '1080:0:0:0:8:800:200C:417A' : '1080::8:800:200c:417a', # a unicast address
|
||||
... 'FF01:0:0:0:0:0:0:43' : 'ff01::43', # a multicast address
|
||||
... '0:0:0:0:0:0:0:1' : '::1', # the loopback address
|
||||
... '0:0:0:0:0:0:0:0' : '::', # the unspecified addresses
|
||||
... }
|
||||
|
||||
>>> for long_form, short_form in valid_addrs.items():
|
||||
... int_val = str_to_int(long_form)
|
||||
... calc_short_form = int_to_str(int_val)
|
||||
... calc_short_form == short_form
|
||||
True
|
||||
True
|
||||
True
|
||||
True
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
IPv6 mapped and compatible IPv4 string formatting.
|
||||
|
||||
{{{
|
||||
|
||||
>>> int_to_str(0xffffff)
|
||||
'::0.255.255.255'
|
||||
>>> int_to_str(0xffffffff)
|
||||
'::255.255.255.255'
|
||||
|
||||
>>> int_to_str(0x1ffffffff)
|
||||
'::1:ffff:ffff'
|
||||
|
||||
>>> int_to_str(0xffffffffffff)
|
||||
'::ffff:255.255.255.255'
|
||||
|
||||
>>> int_to_str(0xfffeffffffff)
|
||||
'::fffe:ffff:ffff'
|
||||
|
||||
>>> int_to_str(0xffffffffffff)
|
||||
'::ffff:255.255.255.255'
|
||||
|
||||
>>> int_to_str(0xfffffffffff1)
|
||||
'::ffff:255.255.255.241'
|
||||
|
||||
>>> int_to_str(0xfffffffffffe)
|
||||
'::ffff:255.255.255.254'
|
||||
|
||||
>>> int_to_str(0xffffffffff00)
|
||||
'::ffff:255.255.255.0'
|
||||
|
||||
>>> int_to_str(0xffffffff0000)
|
||||
'::ffff:255.255.0.0'
|
||||
|
||||
>>> int_to_str(0xffffff000000)
|
||||
'::ffff:255.0.0.0'
|
||||
|
||||
>>> int_to_str(0xffff000000)
|
||||
'::ff:ff00:0'
|
||||
|
||||
>>> int_to_str(0xffff00000000)
|
||||
'::ffff:0.0.0.0'
|
||||
|
||||
>>> int_to_str(0x1ffff00000000)
|
||||
'::1:ffff:0:0'
|
||||
|
||||
>>> int_to_str(0xffff00000000)
|
||||
'::ffff:0.0.0.0'
|
||||
|
||||
}}}
|
||||
|
||||
== str_to_int() Behavioural Tests (legacy_mode switch) ==
|
||||
|
||||
The legacy_mode switch on str_to_int() is for interface compatibility only and should not effect the behaviour of this method whether set to True or False.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str_to_int('::127') == 295
|
||||
True
|
||||
|
||||
>>> str_to_int('::0x7f')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '::0x7f' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::0177') == 375
|
||||
True
|
||||
|
||||
>>> str_to_int('::127.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '::127.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::0x7f.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '::0x7f.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::0177.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AddrFormatError: '::0177.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::127.0.0.1') == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,104 @@
|
||||
=Python 2.x and 3.x compatibility tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.compat import _sys_maxint, _is_str, _is_int, _callable
|
||||
|
||||
>>> from netaddr.compat import _func_doc, _dict_keys, _dict_items
|
||||
|
||||
>>> from netaddr.compat import _iter_dict_keys, _bytes_join, _zip, _range
|
||||
|
||||
>>> from netaddr.compat import _iter_range, _func_name, _func_doc
|
||||
|
||||
# string and integer detection tests.
|
||||
>>> _is_int(_sys_maxint)
|
||||
True
|
||||
|
||||
>>> _is_str(_sys_maxint)
|
||||
False
|
||||
|
||||
>>> _is_str('')
|
||||
True
|
||||
|
||||
>>> _is_str(''.encode())
|
||||
True
|
||||
|
||||
# byte string join tests.
|
||||
>>> str_8bit = _bytes_join(['a'.encode(), 'b'.encode(), 'c'.encode()])
|
||||
|
||||
>>> str_8bit == 'abc'.encode()
|
||||
True
|
||||
|
||||
>>> "b'abc'" == '%r' % str_8bit
|
||||
True
|
||||
|
||||
# dict operation tests.
|
||||
>>> d = { 'a' : 0, 'b' : 1, 'c' : 2 }
|
||||
|
||||
>>> sorted(_dict_keys(d)) == ['a', 'b', 'c']
|
||||
True
|
||||
|
||||
>>> sorted(_dict_items(d)) == [('a', 0), ('b', 1), ('c', 2)]
|
||||
True
|
||||
|
||||
# zip() BIF tests.
|
||||
>>> l2 = _zip([0], [1])
|
||||
|
||||
>>> hasattr(_zip(l2), 'pop')
|
||||
True
|
||||
|
||||
>>> l2 == [(0, 1)]
|
||||
True
|
||||
|
||||
# range/xrange() tests.
|
||||
>>> l1 = _range(3)
|
||||
|
||||
>>> isinstance(l1, list)
|
||||
True
|
||||
|
||||
>>> hasattr(l1, 'pop')
|
||||
True
|
||||
|
||||
>>> l1 == [0, 1, 2]
|
||||
True
|
||||
|
||||
>>> it = _iter_range(3)
|
||||
|
||||
>>> isinstance(it, list)
|
||||
False
|
||||
|
||||
>>> hasattr(it, '__iter__')
|
||||
True
|
||||
|
||||
>>> it == [0, 1, 2]
|
||||
False
|
||||
|
||||
>>> list(it) == [0, 1, 2]
|
||||
True
|
||||
|
||||
# callable() and function meta-data tests.
|
||||
>>> i = 1
|
||||
>>> def f1():
|
||||
... """docstring"""
|
||||
... pass
|
||||
|
||||
>>> f2 = lambda x: x
|
||||
|
||||
>>> _callable(i)
|
||||
False
|
||||
|
||||
>>> _callable(f1)
|
||||
True
|
||||
|
||||
>>> _callable(f2)
|
||||
True
|
||||
|
||||
>>> _func_name(f1) == 'f1'
|
||||
True
|
||||
|
||||
>>> _func_doc(f1) == 'docstring'
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,51 @@
|
||||
=Publish / Subscribe DP Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
Basic Publisher and Subscriber object tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.core import Publisher, Subscriber, PrettyPrinter
|
||||
|
||||
>>> class Subject(Publisher):
|
||||
... pass
|
||||
|
||||
|
||||
>>> class Observer(Subscriber):
|
||||
... def __init__(self, id):
|
||||
... self.id = id
|
||||
...
|
||||
... def update(self, data):
|
||||
... print(repr(self), data)
|
||||
...
|
||||
... def __repr__(self):
|
||||
... return '%s(%r)' % (self.__class__.__name__, self.id)
|
||||
...
|
||||
|
||||
>>> s = Subject()
|
||||
|
||||
>>> s.attach(Observer('foo'))
|
||||
>>> s.attach(Observer('bar'))
|
||||
|
||||
#FIXME: >>> pp = PrettyPrinter()
|
||||
#FIXME: >>> s.attach(pp)
|
||||
|
||||
>>> data = {'foo': 42, 'list': [1,'2', list(range(10))], 'strings': ['foo', 'bar', 'baz', 'quux']}
|
||||
>>> s.notify(data)
|
||||
Observer('foo') {'foo': 42, 'list': [1, '2', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]], 'strings': ['foo', 'bar', 'baz', 'quux']}
|
||||
Observer('bar') {'foo': 42, 'list': [1, '2', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]], 'strings': ['foo', 'bar', 'baz', 'quux']}
|
||||
|
||||
#FIXME: >>> s.detach(pp)
|
||||
>>> s.notify(['foo', 'bar', 'baz'])
|
||||
Observer('foo') ['foo', 'bar', 'baz']
|
||||
Observer('bar') ['foo', 'bar', 'baz']
|
||||
|
||||
>>> s.attach('foo')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: 'foo' does not support required interface!
|
||||
|
||||
>>> s.detach('foo')
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,205 @@
|
||||
=IEEE EUI-64 Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
IEEE EUI-64 tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> eui = EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
>>> eui
|
||||
EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
|
||||
>>> eui.oui
|
||||
OUI('00-1B-77')
|
||||
|
||||
>>> eui.ei
|
||||
'FF-FE-49-54-FD'
|
||||
|
||||
>>> eui.eui64()
|
||||
EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
|
||||
>>> mac = EUI('00-0F-1F-12-E7-33')
|
||||
>>> ip = mac.ipv6_link_local()
|
||||
>>> ip
|
||||
IPAddress('fe80::20f:1fff:fe12:e733')
|
||||
|
||||
>>> mac.eui64()
|
||||
EUI('00-0F-1F-FF-FE-12-E7-33')
|
||||
|
||||
}}}
|
||||
|
||||
Individual Address Block tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> lower_eui = EUI('00-50-C2-05-C0-00')
|
||||
>>> upper_eui = EUI('00-50-C2-05-CF-FF')
|
||||
|
||||
>>> lower_eui.is_iab()
|
||||
True
|
||||
|
||||
>>> str(lower_eui.oui)
|
||||
'00-50-C2'
|
||||
|
||||
>>> str(lower_eui.iab)
|
||||
'00-50-C2-05-C0-00'
|
||||
|
||||
>>> lower_eui.ei
|
||||
'05-C0-00'
|
||||
|
||||
>>> int(lower_eui.oui) == 0x0050c2
|
||||
True
|
||||
|
||||
>>> int(lower_eui.iab) == 0x0050c205c
|
||||
True
|
||||
|
||||
>>> upper_eui.is_iab()
|
||||
True
|
||||
|
||||
>>> str(upper_eui.oui)
|
||||
'00-50-C2'
|
||||
|
||||
>>> str(upper_eui.iab)
|
||||
'00-50-C2-05-C0-00'
|
||||
|
||||
>>> upper_eui.ei
|
||||
'05-CF-FF'
|
||||
|
||||
>>> int(upper_eui.oui) == 0x0050c2
|
||||
True
|
||||
|
||||
>>> int(upper_eui.iab) == 0x0050c205c
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Constructor tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> eui = EUI('00-90-96-AF-CC-39')
|
||||
|
||||
>>> eui == EUI('0-90-96-AF-CC-39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('00-90-96-af-cc-39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('00:90:96:AF:CC:39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('00:90:96:af:cc:39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('0090-96AF-CC39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('0090:96af:cc39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('009096-AFCC39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('009096:AFCC39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('009096AFCC39')
|
||||
True
|
||||
|
||||
>>> eui == EUI('009096afcc39')
|
||||
True
|
||||
|
||||
>>> EUI('01-00-00-00-00-00') == EUI('010000000000')
|
||||
True
|
||||
|
||||
>>> EUI('01-00-00-00-00-00') == EUI('10000000000')
|
||||
True
|
||||
|
||||
>>> EUI('01-00-00-01-00-00') == EUI('010000:010000')
|
||||
True
|
||||
|
||||
>>> EUI('01-00-00-01-00-00') == EUI('10000:10000')
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
EUI-48 and EUI-64 indentifiers of the same value are *not* equivalent.
|
||||
|
||||
{{{
|
||||
|
||||
>>> eui48 = EUI('01-00-00-01-00-00')
|
||||
>>> int(eui48) == 1099511693312
|
||||
True
|
||||
|
||||
>>> eui64 = EUI('00-00-01-00-00-01-00-00')
|
||||
>>> int(eui64) == 1099511693312
|
||||
True
|
||||
|
||||
>>> eui48 == eui64
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Sortability
|
||||
|
||||
{{{
|
||||
|
||||
>>> import random
|
||||
|
||||
>>> eui_list = [EUI(0, 64), EUI(0), EUI(0xffffffffffff, dialect=mac_unix), EUI(0x1000000000000)]
|
||||
|
||||
>>> random.shuffle(eui_list)
|
||||
|
||||
>>> eui_list.sort()
|
||||
|
||||
>>> for eui in eui_list:
|
||||
... str(eui), eui.version
|
||||
('00-00-00-00-00-00', 48)
|
||||
('ff:ff:ff:ff:ff:ff', 48)
|
||||
('00-00-00-00-00-00-00-00', 64)
|
||||
('00-01-00-00-00-00-00-00', 64)
|
||||
|
||||
}}}
|
||||
|
||||
Persistence
|
||||
|
||||
{{{
|
||||
|
||||
>>> import pickle
|
||||
|
||||
>>> eui1 = EUI('00-00-00-01-02-03')
|
||||
>>> eui2 = pickle.loads(pickle.dumps(eui1))
|
||||
>>> eui1 == eui2
|
||||
True
|
||||
|
||||
>>> eui1 = EUI('00-00-00-01-02-03', dialect=mac_cisco)
|
||||
>>> eui2 = pickle.loads(pickle.dumps(eui1))
|
||||
>>> eui1 == eui2
|
||||
True
|
||||
|
||||
>>> eui1.dialect == eui2.dialect
|
||||
True
|
||||
|
||||
>>> oui1 = EUI('00-00-00-01-02-03').oui
|
||||
>>> oui2 = pickle.loads(pickle.dumps(oui1))
|
||||
>>> oui1 == oui2
|
||||
True
|
||||
>>> oui1.records == oui2.records
|
||||
True
|
||||
|
||||
>>> iab1 = EUI('00-50-C2-00-1F-FF').iab
|
||||
>>> iab2 = pickle.loads(pickle.dumps(iab1))
|
||||
>>> iab1 == iab2
|
||||
True
|
||||
>>> iab1.record == iab2.record
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
=IEEE EUI-64 Identifier Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Basic operations.
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
>>> mac
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> eui = mac.eui64()
|
||||
>>> eui
|
||||
EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
|
||||
>>> int(eui) == 7731765737772285
|
||||
True
|
||||
|
||||
>>> eui.packed
|
||||
b'\x00\x1bw\xff\xfeIT\xfd'
|
||||
|
||||
>>> eui.bin
|
||||
'0b11011011101111111111111111110010010010101010011111101'
|
||||
|
||||
>>> eui.bits()
|
||||
'00000000-00011011-01110111-11111111-11111110-01001001-01010100-11111101'
|
||||
|
||||
}}}
|
||||
|
||||
IPv6 interoperability
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> eui = mac.eui64()
|
||||
|
||||
>>> mac
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> eui
|
||||
EUI('00-1B-77-FF-FE-49-54-FD')
|
||||
|
||||
>>> mac.ipv6_link_local()
|
||||
IPAddress('fe80::21b:77ff:fe49:54fd')
|
||||
|
||||
>>> eui.ipv6_link_local()
|
||||
IPAddress('fe80::21b:77ff:fe49:54fd')
|
||||
@@ -0,0 +1,56 @@
|
||||
=IEEE Publish/Subscribe Parser Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
Basic OUIIndexParser and FileIndexer object tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.eui.ieee import OUIIndexParser, IABIndexParser, FileIndexer
|
||||
>>> from io import StringIO
|
||||
|
||||
>>> infile = StringIO()
|
||||
>>> outfile = StringIO()
|
||||
>>> infile.write("""
|
||||
... 00-CA-FE (hex) ACME CORPORATION
|
||||
... 00CAFE (base 16) ACME CORPORATION
|
||||
... 1 MAIN STREET
|
||||
... SPRINGFIELD
|
||||
... UNITED STATES
|
||||
... """)
|
||||
211
|
||||
>>> infile.seek(0)
|
||||
0
|
||||
>>> iab_parser = OUIIndexParser(infile)
|
||||
>>> iab_parser.attach(FileIndexer(outfile))
|
||||
>>> iab_parser.parse()
|
||||
>>> print(outfile.getvalue())
|
||||
51966,1,210
|
||||
<BLANKLINE>
|
||||
|
||||
}}}
|
||||
|
||||
Basic IABIndexParser and FileIndexer object tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> infile = StringIO()
|
||||
>>> outfile = StringIO()
|
||||
>>> infile.write("""
|
||||
... 00-50-C2 (hex) ACME CORPORATION
|
||||
... ABC000-ABCFFF (base 16) ACME CORPORATION
|
||||
... 1 MAIN STREET
|
||||
... SPRINGFIELD
|
||||
... UNITED STATES
|
||||
... """)
|
||||
182
|
||||
>>> infile.seek(0)
|
||||
0
|
||||
>>> iab_parser = IABIndexParser(infile)
|
||||
>>> iab_parser.attach(FileIndexer(outfile))
|
||||
>>> iab_parser.parse()
|
||||
>>> print(outfile.getvalue())
|
||||
84683452,1,181
|
||||
<BLANKLINE>
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,254 @@
|
||||
=EUI (MAC) Address Tutorial=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Operations==
|
||||
|
||||
This EUI object represents a MAC address.
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
|
||||
}}}
|
||||
|
||||
Standard repr() access returns a Python statement that can reconstruct the MAC address object from scratch if executed in the Python interpreter.
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
}}}
|
||||
|
||||
Accessing the EUI object in the string context.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str(mac)
|
||||
'00-1B-77-49-54-FD'
|
||||
|
||||
>>> '%s' % mac
|
||||
'00-1B-77-49-54-FD'
|
||||
|
||||
}}}
|
||||
|
||||
Here are a few other common properties.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str(mac), str(mac.oui), mac.ei, mac.version
|
||||
('00-1B-77-49-54-FD', '00-1B-77', '49-54-FD', 48)
|
||||
|
||||
}}}
|
||||
|
||||
==MAC Address Numerical Representations==
|
||||
|
||||
You can view an individual IP address in various other formats.
|
||||
|
||||
{{{
|
||||
|
||||
>>> int(mac) == 117965411581
|
||||
True
|
||||
|
||||
>>> hex(mac)
|
||||
'0x1b774954fd'
|
||||
|
||||
>>> oct(mac)
|
||||
'0o1556722252375'
|
||||
|
||||
>>> mac.bits()
|
||||
'00000000-00011011-01110111-01001001-01010100-11111101'
|
||||
|
||||
>>> mac.bin
|
||||
'0b1101101110111010010010101010011111101'
|
||||
|
||||
}}}
|
||||
|
||||
==MAC Address Formatting==
|
||||
|
||||
It is very common to see MAC address in many different formats other than the standard IEEE EUI-48.
|
||||
|
||||
The EUI class constructor handles all these common forms.
|
||||
|
||||
{{{
|
||||
|
||||
>>> EUI('00-1B-77-49-54-FD')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
IEEE EUI-48 lowercase format.
|
||||
|
||||
>>> EUI('00-1b-77-49-54-fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
Common UNIX format.
|
||||
|
||||
>>> EUI('0:1b:77:49:54:fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
Cisco triple hextet format.
|
||||
|
||||
>>> EUI('001b:7749:54fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> EUI('1b:7749:54fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> EUI('1B:7749:54FD')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
Bare MAC addresses (no delimiters).
|
||||
|
||||
>>> EUI('001b774954fd')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> EUI('01B774954FD')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
PostreSQL format (found in documentation).
|
||||
|
||||
>>> EUI('001B77:4954FD')
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
}}}
|
||||
|
||||
It is equally possible to specify a selected format for your MAC string output in the form of a 'dialect' class. It's use is similar to the dialect class used in the Python standard library csv module.
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> mac
|
||||
EUI('00-1B-77-49-54-FD')
|
||||
|
||||
>>> mac.dialect = mac_unix
|
||||
>>> mac
|
||||
EUI('0:1b:77:49:54:fd')
|
||||
|
||||
>>> mac.dialect = mac_cisco
|
||||
>>> mac
|
||||
EUI('001b.7749.54fd')
|
||||
|
||||
>>> mac.dialect = mac_bare
|
||||
>>> mac
|
||||
EUI('001B774954FD')
|
||||
|
||||
>>> mac.dialect = mac_pgsql
|
||||
>>> mac
|
||||
EUI('001b77:4954fd')
|
||||
|
||||
}}}
|
||||
|
||||
You can of course, create your own dialect classes to customise the MAC formatting if the standard ones do not suit your needs.
|
||||
|
||||
Here's a tweaked UNIX MAC dialect that generates uppercase, zero-filled octets.
|
||||
|
||||
{{{
|
||||
|
||||
>>> class mac_custom(mac_unix): pass
|
||||
>>> mac_custom.word_fmt = '%.2X'
|
||||
>>> mac = EUI('00-1B-77-49-54-FD', dialect=mac_custom)
|
||||
>>> mac
|
||||
EUI('00:1B:77:49:54:FD')
|
||||
|
||||
}}}
|
||||
|
||||
==Looking Up EUI Organisational Data==
|
||||
|
||||
EUI objects provide an interface to the OUI (Organisationally Unique Identifier) and IAB (Individual Address Block) registration databases available from the IEEE.
|
||||
|
||||
Here is how you query an OUI with the EUI interface.
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac = EUI('00-1B-77-49-54-FD')
|
||||
>>> oui = mac.oui
|
||||
>>> oui
|
||||
OUI('00-1B-77')
|
||||
|
||||
>>> oui.registration().address
|
||||
['Lot 8, Jalan Hi-Tech 2/3', 'Kulim Hi-Tech Park', 'Kulim Kedah 09000', 'MALAYSIA']
|
||||
|
||||
>>> oui.registration().org
|
||||
'Intel Corporate'
|
||||
|
||||
}}}
|
||||
|
||||
You can also use OUI objects directly without going through the EUI interface.
|
||||
|
||||
A few OUI records have multiple registrations against them. I'm not sure if this is recording historical information or just a quirk of the IEEE reigstration process.
|
||||
|
||||
This example show you how you access them individually by specifying an index number.
|
||||
|
||||
{{{
|
||||
|
||||
>>> oui = OUI(524336) # OUI constructor accepts integer values too.
|
||||
|
||||
>>> oui
|
||||
OUI('08-00-30')
|
||||
|
||||
>>> oui.registration(0).address
|
||||
['2380 N. ROSE AVENUE', 'OXNARD CA 93010', 'UNITED STATES']
|
||||
|
||||
>>> oui.registration(0).org
|
||||
'NETWORK RESEARCH CORPORATION'
|
||||
|
||||
>>> oui.registration(0).oui
|
||||
'08-00-30'
|
||||
|
||||
>>> oui.registration(1).address
|
||||
['CH-1211 GENEVE 23', 'SUISSE/SWITZ', 'SWITZERLAND']
|
||||
|
||||
>>> oui.registration(1).org
|
||||
'CERN'
|
||||
|
||||
>>> oui.registration(1).oui
|
||||
'08-00-30'
|
||||
|
||||
>>> oui.registration(2).address
|
||||
['GPO BOX 2476V', 'MELBOURNE VIC 3001', 'AUSTRALIA']
|
||||
|
||||
>>> oui.registration(2).org
|
||||
'ROYAL MELBOURNE INST OF TECH'
|
||||
|
||||
>>> oui.registration(2).oui
|
||||
'08-00-30'
|
||||
|
||||
>>> for i in range(oui.reg_count):
|
||||
... str(oui), oui.registration(i).org
|
||||
...
|
||||
('08-00-30', 'NETWORK RESEARCH CORPORATION')
|
||||
('08-00-30', 'CERN')
|
||||
('08-00-30', 'ROYAL MELBOURNE INST OF TECH')
|
||||
|
||||
}}}
|
||||
|
||||
Here is how you query an IAB with the EUI interface.
|
||||
|
||||
{{{
|
||||
|
||||
>>> mac = EUI('00-50-C2-00-0F-01')
|
||||
|
||||
>>> mac.is_iab()
|
||||
True
|
||||
|
||||
>>> iab = mac.iab
|
||||
|
||||
>>> iab
|
||||
IAB('00-50-C2-00-00-00')
|
||||
|
||||
>>> iab.registration()
|
||||
{'address': ['2101 Superior Avenue', 'Cleveland OH 44114', 'UNITED STATES'],
|
||||
'iab': '00-50-C2-00-00-00',
|
||||
...
|
||||
'offset': 68,
|
||||
'org': 'T.L.S. Corp.',
|
||||
'size': 133}
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,202 @@
|
||||
=Abbreviated CIDR Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Abbreviation tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ranges = (
|
||||
... (IPAddress('::'), IPAddress('::')),
|
||||
... (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')),
|
||||
... (IPAddress('::'), IPAddress('::255.255.255.255')),
|
||||
... (IPAddress('0.0.0.0'), IPAddress('0.0.0.0')),
|
||||
... )
|
||||
|
||||
>>> sorted(ranges)
|
||||
[(IPAddress('0.0.0.0'), IPAddress('0.0.0.0')), (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')), (IPAddress('::'), IPAddress('::')), (IPAddress('::'), IPAddress('::255.255.255.255'))]
|
||||
|
||||
# Integer values.
|
||||
>>> cidr_abbrev_to_verbose(-1)
|
||||
-1
|
||||
|
||||
# Class A
|
||||
>>> cidr_abbrev_to_verbose(0)
|
||||
'0.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose(10)
|
||||
'10.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose(127)
|
||||
'127.0.0.0/8'
|
||||
|
||||
# Class B
|
||||
>>> cidr_abbrev_to_verbose(128)
|
||||
'128.0.0.0/16'
|
||||
>>> cidr_abbrev_to_verbose(191)
|
||||
'191.0.0.0/16'
|
||||
|
||||
# Class C
|
||||
>>> cidr_abbrev_to_verbose(192)
|
||||
'192.0.0.0/24'
|
||||
>>> cidr_abbrev_to_verbose(223)
|
||||
'223.0.0.0/24'
|
||||
|
||||
# Class D (multicast)
|
||||
>>> cidr_abbrev_to_verbose(224)
|
||||
'224.0.0.0/4'
|
||||
>>> cidr_abbrev_to_verbose(225)
|
||||
'225.0.0.0/4'
|
||||
>>> cidr_abbrev_to_verbose(239)
|
||||
'239.0.0.0/4'
|
||||
|
||||
# Class E (reserved)
|
||||
>>> cidr_abbrev_to_verbose(240)
|
||||
'240.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose(254)
|
||||
'254.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose(255)
|
||||
'255.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose(256)
|
||||
256
|
||||
|
||||
# String values.
|
||||
>>> cidr_abbrev_to_verbose('-1')
|
||||
'-1'
|
||||
|
||||
# Class A
|
||||
>>> cidr_abbrev_to_verbose('0')
|
||||
'0.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('10')
|
||||
'10.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('127')
|
||||
'127.0.0.0/8'
|
||||
|
||||
# Class B
|
||||
>>> cidr_abbrev_to_verbose('128')
|
||||
'128.0.0.0/16'
|
||||
>>> cidr_abbrev_to_verbose('191')
|
||||
'191.0.0.0/16'
|
||||
|
||||
# Class C
|
||||
>>> cidr_abbrev_to_verbose('192')
|
||||
'192.0.0.0/24'
|
||||
>>> cidr_abbrev_to_verbose('223')
|
||||
'223.0.0.0/24'
|
||||
|
||||
# Class D (multicast)
|
||||
>>> cidr_abbrev_to_verbose('224')
|
||||
'224.0.0.0/4'
|
||||
>>> cidr_abbrev_to_verbose('225')
|
||||
'225.0.0.0/4'
|
||||
>>> cidr_abbrev_to_verbose('239')
|
||||
'239.0.0.0/4'
|
||||
|
||||
# Class E (reserved)
|
||||
>>> cidr_abbrev_to_verbose('240')
|
||||
'240.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose('254')
|
||||
'254.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose('255')
|
||||
'255.0.0.0/32'
|
||||
>>> cidr_abbrev_to_verbose('256')
|
||||
'256'
|
||||
|
||||
>>> cidr_abbrev_to_verbose('128/8')
|
||||
'128.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('128.0/8')
|
||||
'128.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('128.0.0.0/8')
|
||||
'128.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('128.0.0/8')
|
||||
'128.0.0.0/8'
|
||||
>>> cidr_abbrev_to_verbose('192.168')
|
||||
'192.168.0.0/24'
|
||||
>>> cidr_abbrev_to_verbose('192.0.2')
|
||||
'192.0.2.0/24'
|
||||
>>> cidr_abbrev_to_verbose('192.0.2.0')
|
||||
'192.0.2.0/24'
|
||||
>>> cidr_abbrev_to_verbose('0.0.0.0')
|
||||
'0.0.0.0/8'
|
||||
|
||||
# No IPv6 support current.
|
||||
>>> cidr_abbrev_to_verbose('::/128')
|
||||
'::/128'
|
||||
|
||||
# IPv6 proper, not IPv4 mapped?
|
||||
>>> cidr_abbrev_to_verbose('::10/128')
|
||||
'::10/128'
|
||||
>>> cidr_abbrev_to_verbose('0.0.0.0.0')
|
||||
'0.0.0.0.0'
|
||||
>>> cidr_abbrev_to_verbose('')
|
||||
''
|
||||
>>> cidr_abbrev_to_verbose(None)
|
||||
|
||||
>>> cidr_abbrev_to_verbose([])
|
||||
[]
|
||||
>>> cidr_abbrev_to_verbose({})
|
||||
{}
|
||||
|
||||
}}}
|
||||
|
||||
Negative testing.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_abbrev_to_verbose('192.0.2.0')
|
||||
'192.0.2.0/24'
|
||||
|
||||
>>> cidr_abbrev_to_verbose('192.0.2.0/32')
|
||||
'192.0.2.0/32'
|
||||
|
||||
#FIXME: >>> cidr_abbrev_to_verbose('192.0.2.0/33')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: prefixlen in address '192.0.2.0/33' out of range for IPv4!
|
||||
|
||||
}}}
|
||||
|
||||
IPv4 octet expansion routine.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy import ipv4
|
||||
|
||||
>>> ipv4.expand_partial_address('10')
|
||||
'10.0.0.0'
|
||||
|
||||
>>> ipv4.expand_partial_address('10.1')
|
||||
'10.1.0.0'
|
||||
|
||||
>>> ipv4.expand_partial_address('192.168.1')
|
||||
'192.168.1.0'
|
||||
|
||||
}}}
|
||||
|
||||
IPNetwork constructor testing.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.168/16')
|
||||
IPNetwork('192.168.0.0/16')
|
||||
|
||||
>>> IPNetwork('192.168.0.15')
|
||||
IPNetwork('192.168.0.15/32')
|
||||
|
||||
>>> IPNetwork('192.168')
|
||||
IPNetwork('192.168.0.0/32')
|
||||
|
||||
>>> IPNetwork('192.168', implicit_prefix=True)
|
||||
IPNetwork('192.168.0.0/24')
|
||||
|
||||
>>> IPNetwork('192.168', True)
|
||||
IPNetwork('192.168.0.0/24')
|
||||
|
||||
>>> IPNetwork('10.0.0.1', True)
|
||||
IPNetwork('10.0.0.1/8')
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,44 @@
|
||||
=Binary and numerical operations on IP addresses=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
==Addition and Subtraction with integers ==
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.0') + 1
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> 1 + IPAddress('192.0.2.0')
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> IPAddress('192.0.2.1') - 1
|
||||
IPAddress('192.0.2.0')
|
||||
|
||||
>>> 1 - IPAddress('192.0.2.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: result outside valid IP address boundary!
|
||||
|
||||
}}}
|
||||
|
||||
==Binary operations==
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.15') & IPAddress('255.255.255.0')
|
||||
IPAddress('192.0.2.0')
|
||||
|
||||
>>> IPAddress('255.255.0.0') | IPAddress('0.0.255.255')
|
||||
IPAddress('255.255.255.255')
|
||||
|
||||
>>> IPAddress('255.255.0.0') ^ IPAddress('255.0.0.0')
|
||||
IPAddress('0.255.0.0')
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,157 @@
|
||||
=IP Range Boundary Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
>>> import pprint
|
||||
|
||||
}}}
|
||||
|
||||
`iter_iprange()` iterator boundary tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> pprint.pprint(list(iter_iprange('192.0.2.0', '192.0.2.7')))
|
||||
[IPAddress('192.0.2.0'),
|
||||
IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.6'),
|
||||
IPAddress('192.0.2.7')]
|
||||
|
||||
>>> pprint.pprint(list(iter_iprange('::ffff:192.0.2.0', '::ffff:192.0.2.7')))
|
||||
[IPAddress('::ffff:192.0.2.0'),
|
||||
IPAddress('::ffff:192.0.2.1'),
|
||||
IPAddress('::ffff:192.0.2.2'),
|
||||
IPAddress('::ffff:192.0.2.3'),
|
||||
IPAddress('::ffff:192.0.2.4'),
|
||||
IPAddress('::ffff:192.0.2.5'),
|
||||
IPAddress('::ffff:192.0.2.6'),
|
||||
IPAddress('::ffff:192.0.2.7')]
|
||||
|
||||
|
||||
}}}
|
||||
|
||||
`IPNetwork()` iterator boundary tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> pprint.pprint(list(IPNetwork('192.0.2.0/29')[0:-1]))
|
||||
[IPAddress('192.0.2.0'),
|
||||
IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.6')]
|
||||
|
||||
>>> pprint.pprint(list(IPNetwork('192.0.2.0/29')[::-1]))
|
||||
[IPAddress('192.0.2.7'),
|
||||
IPAddress('192.0.2.6'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.0')]
|
||||
|
||||
>>> pprint.pprint(list(IPNetwork('192.0.2.0/29').iter_hosts()))
|
||||
[IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.6')]
|
||||
|
||||
>>> pprint.pprint(list(IPNetwork('::ffff:192.0.2.0/125').iter_hosts()))
|
||||
[IPAddress('::ffff:192.0.2.0'),
|
||||
IPAddress('::ffff:192.0.2.1'),
|
||||
IPAddress('::ffff:192.0.2.2'),
|
||||
IPAddress('::ffff:192.0.2.3'),
|
||||
IPAddress('::ffff:192.0.2.4'),
|
||||
IPAddress('::ffff:192.0.2.5'),
|
||||
IPAddress('::ffff:192.0.2.6'),
|
||||
IPAddress('::ffff:192.0.2.7')]
|
||||
|
||||
}}}
|
||||
|
||||
`IPRange()` iterator boundary tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> pprint.pprint(list(IPRange('192.0.2.0', '192.0.2.7')))
|
||||
[IPAddress('192.0.2.0'),
|
||||
IPAddress('192.0.2.1'),
|
||||
IPAddress('192.0.2.2'),
|
||||
IPAddress('192.0.2.3'),
|
||||
IPAddress('192.0.2.4'),
|
||||
IPAddress('192.0.2.5'),
|
||||
IPAddress('192.0.2.6'),
|
||||
IPAddress('192.0.2.7')]
|
||||
|
||||
>>> pprint.pprint(list(IPRange('::ffff:192.0.2.0', '::ffff:192.0.2.7')))
|
||||
[IPAddress('::ffff:192.0.2.0'),
|
||||
IPAddress('::ffff:192.0.2.1'),
|
||||
IPAddress('::ffff:192.0.2.2'),
|
||||
IPAddress('::ffff:192.0.2.3'),
|
||||
IPAddress('::ffff:192.0.2.4'),
|
||||
IPAddress('::ffff:192.0.2.5'),
|
||||
IPAddress('::ffff:192.0.2.6'),
|
||||
IPAddress('::ffff:192.0.2.7')]
|
||||
|
||||
}}}
|
||||
|
||||
Boolean contexts.
|
||||
|
||||
{{{
|
||||
|
||||
>>> bool(IPAddress('0.0.0.0'))
|
||||
False
|
||||
|
||||
>>> bool(IPAddress('0.0.0.1'))
|
||||
True
|
||||
|
||||
>>> bool(IPAddress('255.255.255.255'))
|
||||
True
|
||||
|
||||
>>> bool(IPNetwork('0.0.0.0/0'))
|
||||
True
|
||||
|
||||
>>> bool(IPNetwork('::/0'))
|
||||
True
|
||||
|
||||
>>> bool(IPRange('0.0.0.0', '255.255.255.255'))
|
||||
True
|
||||
|
||||
>>> bool(IPRange('0.0.0.0', '0.0.0.0'))
|
||||
True
|
||||
|
||||
>>> bool(IPGlob('*.*.*.*'))
|
||||
True
|
||||
|
||||
>>> bool(IPGlob('0.0.0.0'))
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
`IPAddress()` negative increment tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress('0.0.0.0')
|
||||
>>> ip += -1
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: result outside valid IP address boundary!
|
||||
|
||||
>>> ip = IPAddress('255.255.255.255')
|
||||
>>> ip -= -1
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: result outside valid IP address boundary!
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,449 @@
|
||||
=CIDR Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic IP Range Tuple Sorting==
|
||||
|
||||
{{{
|
||||
|
||||
>>> ranges = (
|
||||
... (IPAddress('::'), IPAddress('::')),
|
||||
... (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')),
|
||||
... (IPAddress('::'), IPAddress('::255.255.255.255')),
|
||||
... (IPAddress('0.0.0.0'), IPAddress('0.0.0.0')),
|
||||
... )
|
||||
|
||||
>>> sorted(ranges)
|
||||
[(IPAddress('0.0.0.0'), IPAddress('0.0.0.0')), (IPAddress('0.0.0.0'), IPAddress('255.255.255.255')), (IPAddress('::'), IPAddress('::')), (IPAddress('::'), IPAddress('::255.255.255.255'))]
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 range to CIDR conversion.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('0.0.0.1', '255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
IPNetwork('0.0.0.1/32')
|
||||
IPNetwork('0.0.0.2/31')
|
||||
IPNetwork('0.0.0.4/30')
|
||||
IPNetwork('0.0.0.8/29')
|
||||
IPNetwork('0.0.0.16/28')
|
||||
IPNetwork('0.0.0.32/27')
|
||||
IPNetwork('0.0.0.64/26')
|
||||
IPNetwork('0.0.0.128/25')
|
||||
IPNetwork('0.0.1.0/24')
|
||||
IPNetwork('0.0.2.0/23')
|
||||
IPNetwork('0.0.4.0/22')
|
||||
IPNetwork('0.0.8.0/21')
|
||||
IPNetwork('0.0.16.0/20')
|
||||
IPNetwork('0.0.32.0/19')
|
||||
IPNetwork('0.0.64.0/18')
|
||||
IPNetwork('0.0.128.0/17')
|
||||
IPNetwork('0.1.0.0/16')
|
||||
IPNetwork('0.2.0.0/15')
|
||||
IPNetwork('0.4.0.0/14')
|
||||
IPNetwork('0.8.0.0/13')
|
||||
IPNetwork('0.16.0.0/12')
|
||||
IPNetwork('0.32.0.0/11')
|
||||
IPNetwork('0.64.0.0/10')
|
||||
IPNetwork('0.128.0.0/9')
|
||||
IPNetwork('1.0.0.0/8')
|
||||
IPNetwork('2.0.0.0/7')
|
||||
IPNetwork('4.0.0.0/6')
|
||||
IPNetwork('8.0.0.0/5')
|
||||
IPNetwork('16.0.0.0/4')
|
||||
IPNetwork('32.0.0.0/3')
|
||||
IPNetwork('64.0.0.0/2')
|
||||
IPNetwork('128.0.0.0/2')
|
||||
IPNetwork('192.0.0.0/3')
|
||||
IPNetwork('224.0.0.0/4')
|
||||
IPNetwork('240.0.0.0/5')
|
||||
IPNetwork('248.0.0.0/6')
|
||||
IPNetwork('252.0.0.0/7')
|
||||
IPNetwork('254.0.0.0/8')
|
||||
IPNetwork('255.0.0.0/9')
|
||||
IPNetwork('255.128.0.0/10')
|
||||
IPNetwork('255.192.0.0/11')
|
||||
IPNetwork('255.224.0.0/12')
|
||||
IPNetwork('255.240.0.0/13')
|
||||
IPNetwork('255.248.0.0/14')
|
||||
IPNetwork('255.252.0.0/15')
|
||||
IPNetwork('255.254.0.0/16')
|
||||
IPNetwork('255.255.0.0/17')
|
||||
IPNetwork('255.255.128.0/18')
|
||||
IPNetwork('255.255.192.0/19')
|
||||
IPNetwork('255.255.224.0/20')
|
||||
IPNetwork('255.255.240.0/21')
|
||||
IPNetwork('255.255.248.0/22')
|
||||
IPNetwork('255.255.252.0/23')
|
||||
IPNetwork('255.255.254.0/24')
|
||||
IPNetwork('255.255.255.0/25')
|
||||
IPNetwork('255.255.255.128/26')
|
||||
IPNetwork('255.255.255.192/27')
|
||||
IPNetwork('255.255.255.224/28')
|
||||
IPNetwork('255.255.255.240/29')
|
||||
IPNetwork('255.255.255.248/30')
|
||||
IPNetwork('255.255.255.252/31')
|
||||
IPNetwork('255.255.255.254/32')
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 mapped IPv6 range to CIDR.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('::ffff:1', '::ffff:255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
IPNetwork('::255.255.0.1/128')
|
||||
IPNetwork('::255.255.0.2/127')
|
||||
IPNetwork('::255.255.0.4/126')
|
||||
IPNetwork('::255.255.0.8/125')
|
||||
IPNetwork('::255.255.0.16/124')
|
||||
IPNetwork('::255.255.0.32/123')
|
||||
IPNetwork('::255.255.0.64/122')
|
||||
IPNetwork('::255.255.0.128/121')
|
||||
IPNetwork('::255.255.1.0/120')
|
||||
IPNetwork('::255.255.2.0/119')
|
||||
IPNetwork('::255.255.4.0/118')
|
||||
IPNetwork('::255.255.8.0/117')
|
||||
IPNetwork('::255.255.16.0/116')
|
||||
IPNetwork('::255.255.32.0/115')
|
||||
IPNetwork('::255.255.64.0/114')
|
||||
IPNetwork('::255.255.128.0/113')
|
||||
IPNetwork('::1:0:0/96')
|
||||
IPNetwork('::2:0:0/95')
|
||||
IPNetwork('::4:0:0/94')
|
||||
IPNetwork('::8:0:0/93')
|
||||
IPNetwork('::10:0:0/92')
|
||||
IPNetwork('::20:0:0/91')
|
||||
IPNetwork('::40:0:0/90')
|
||||
IPNetwork('::80:0:0/89')
|
||||
IPNetwork('::100:0:0/88')
|
||||
IPNetwork('::200:0:0/87')
|
||||
IPNetwork('::400:0:0/86')
|
||||
IPNetwork('::800:0:0/85')
|
||||
IPNetwork('::1000:0:0/84')
|
||||
IPNetwork('::2000:0:0/83')
|
||||
IPNetwork('::4000:0:0/82')
|
||||
IPNetwork('::8000:0:0/82')
|
||||
IPNetwork('::c000:0:0/83')
|
||||
IPNetwork('::e000:0:0/84')
|
||||
IPNetwork('::f000:0:0/85')
|
||||
IPNetwork('::f800:0:0/86')
|
||||
IPNetwork('::fc00:0:0/87')
|
||||
IPNetwork('::fe00:0:0/88')
|
||||
IPNetwork('::ff00:0:0/89')
|
||||
IPNetwork('::ff80:0:0/90')
|
||||
IPNetwork('::ffc0:0:0/91')
|
||||
IPNetwork('::ffe0:0:0/92')
|
||||
IPNetwork('::fff0:0:0/93')
|
||||
IPNetwork('::fff8:0:0/94')
|
||||
IPNetwork('::fffc:0:0/95')
|
||||
IPNetwork('::fffe:0:0/96')
|
||||
IPNetwork('::ffff:0.0.0.0/97')
|
||||
IPNetwork('::ffff:128.0.0.0/98')
|
||||
IPNetwork('::ffff:192.0.0.0/99')
|
||||
IPNetwork('::ffff:224.0.0.0/100')
|
||||
IPNetwork('::ffff:240.0.0.0/101')
|
||||
IPNetwork('::ffff:248.0.0.0/102')
|
||||
IPNetwork('::ffff:252.0.0.0/103')
|
||||
IPNetwork('::ffff:254.0.0.0/104')
|
||||
IPNetwork('::ffff:255.0.0.0/105')
|
||||
IPNetwork('::ffff:255.128.0.0/106')
|
||||
IPNetwork('::ffff:255.192.0.0/107')
|
||||
IPNetwork('::ffff:255.224.0.0/108')
|
||||
IPNetwork('::ffff:255.240.0.0/109')
|
||||
IPNetwork('::ffff:255.248.0.0/110')
|
||||
IPNetwork('::ffff:255.252.0.0/111')
|
||||
IPNetwork('::ffff:255.254.0.0/112')
|
||||
IPNetwork('::ffff:255.255.0.0/113')
|
||||
IPNetwork('::ffff:255.255.128.0/114')
|
||||
IPNetwork('::ffff:255.255.192.0/115')
|
||||
IPNetwork('::ffff:255.255.224.0/116')
|
||||
IPNetwork('::ffff:255.255.240.0/117')
|
||||
IPNetwork('::ffff:255.255.248.0/118')
|
||||
IPNetwork('::ffff:255.255.252.0/119')
|
||||
IPNetwork('::ffff:255.255.254.0/120')
|
||||
IPNetwork('::ffff:255.255.255.0/121')
|
||||
IPNetwork('::ffff:255.255.255.128/122')
|
||||
IPNetwork('::ffff:255.255.255.192/123')
|
||||
IPNetwork('::ffff:255.255.255.224/124')
|
||||
IPNetwork('::ffff:255.255.255.240/125')
|
||||
IPNetwork('::ffff:255.255.255.248/126')
|
||||
IPNetwork('::ffff:255.255.255.252/127')
|
||||
IPNetwork('::ffff:255.255.255.254/128')
|
||||
|
||||
}}}
|
||||
|
||||
RFC 4291 CIDR tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str(IPNetwork('2001:0DB8:0000:CD30:0000:0000:0000:0000/60'))
|
||||
'2001:db8:0:cd30::/60'
|
||||
|
||||
>>> str(IPNetwork('2001:0DB8::CD30:0:0:0:0/60'))
|
||||
'2001:db8:0:cd30::/60'
|
||||
|
||||
>>> str(IPNetwork('2001:0DB8:0:CD30::/60'))
|
||||
'2001:db8:0:cd30::/60'
|
||||
|
||||
}}}
|
||||
|
||||
Equality tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0/255.255.254.0') == IPNetwork('192.0.2.0/23')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.65/255.255.254.0') == IPNetwork('192.0.2.0/23')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.65/255.255.254.0') == IPNetwork('192.0.2.65/23')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.65/255.255.255.0') == IPNetwork('192.0.2.0/23')
|
||||
False
|
||||
|
||||
>>> IPNetwork('192.0.2.65/255.255.254.0') == IPNetwork('192.0.2.65/24')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Slicing tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.0/23')
|
||||
>>> ip.first == 3221225984
|
||||
True
|
||||
|
||||
>>> ip.last == 3221226495
|
||||
True
|
||||
|
||||
>>> ip[0]
|
||||
IPAddress('192.0.2.0')
|
||||
|
||||
>>> ip[-1]
|
||||
IPAddress('192.0.3.255')
|
||||
|
||||
>>> list(ip[::128])
|
||||
[IPAddress('192.0.2.0'), IPAddress('192.0.2.128'), IPAddress('192.0.3.0'), IPAddress('192.0.3.128')]
|
||||
|
||||
>>> ip = IPNetwork('fe80::/10')
|
||||
>>> ip[0]
|
||||
IPAddress('fe80::')
|
||||
|
||||
>>> ip[-1]
|
||||
IPAddress('febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
|
||||
|
||||
>>> ip.size == 332306998946228968225951765070086144
|
||||
True
|
||||
|
||||
>>> list(ip[0:5:1])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: IPv6 slices are not supported!
|
||||
|
||||
|
||||
}}}
|
||||
|
||||
Membership tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.1') in IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.255') in IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') in IPNetwork('192.0.2.0/23')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') in IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPAddress('ffff::1') in IPNetwork('ffff::/127')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/23') in IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Equality tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') == IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') is not IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') != IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') is IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
>>> IPNetwork('fe80::/10') == IPNetwork('fe80::/10')
|
||||
True
|
||||
|
||||
>>> IPNetwork('fe80::/10') is not IPNetwork('fe80::/10')
|
||||
True
|
||||
|
||||
>>> IPNetwork('fe80::/10') != IPNetwork('fe80::/10')
|
||||
False
|
||||
|
||||
>>> IPNetwork('fe80::/10') is IPNetwork('fe80::/10')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Exclusion tests.
|
||||
|
||||
{{{
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1]) - set([1])
|
||||
# set([1])
|
||||
>>> cidr_exclude('192.0.2.1/32', '192.0.2.1/32')
|
||||
[]
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1,2]) - set([2])
|
||||
# set([1])
|
||||
>>> cidr_exclude('192.0.2.0/31', '192.0.2.1/32')
|
||||
[IPNetwork('192.0.2.0/32')]
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1,2,3,4,5,6,7,8]) - set([5,6,7,8])
|
||||
# set([1, 2, 3, 4])
|
||||
>>> cidr_exclude('192.0.2.0/24', '192.0.2.128/25')
|
||||
[IPNetwork('192.0.2.0/25')]
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1,2,3,4,5,6,7,8]) - set([5,6])
|
||||
# set([1, 2, 3, 4, 7, 8])
|
||||
>>> cidr_exclude('192.0.2.0/24', '192.0.2.128/27')
|
||||
[IPNetwork('192.0.2.0/25'), IPNetwork('192.0.2.160/27'), IPNetwork('192.0.2.192/26')]
|
||||
|
||||
# Subtracting a larger range from a smaller one results in an empty
|
||||
# list (rather than a negative CIDR - which would be rather odd)!
|
||||
#
|
||||
# Equivalent to :-
|
||||
# >>> set([1]) - set([1,2,3])
|
||||
# set([])
|
||||
>>> cidr_exclude('192.0.2.1/32', '192.0.2.0/24')
|
||||
[]
|
||||
|
||||
}}}
|
||||
|
||||
Please Note: excluding IP subnets that are not within each other and have no overlaps should return the original target IP object.
|
||||
|
||||
{{{
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1,2,3]) - set([4])
|
||||
# set([1,2,3])
|
||||
>>> cidr_exclude('192.0.2.0/28', '192.0.2.16/32')
|
||||
[IPNetwork('192.0.2.0/28')]
|
||||
|
||||
# Equivalent to :-
|
||||
# >>> set([1]) - set([2,3,4])
|
||||
# set([1])
|
||||
>>> cidr_exclude('192.0.1.255/32', '192.0.2.0/28')
|
||||
[IPNetwork('192.0.1.255/32')]
|
||||
|
||||
}}}
|
||||
|
||||
Merge tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(['192.0.128.0/24', '192.0.129.0/24'])
|
||||
[IPNetwork('192.0.128.0/23')]
|
||||
|
||||
>>> cidr_merge(['192.0.129.0/24', '192.0.130.0/24'])
|
||||
[IPNetwork('192.0.129.0/24'), IPNetwork('192.0.130.0/24')]
|
||||
|
||||
>>> cidr_merge(['192.0.2.112/30', '192.0.2.116/31', '192.0.2.118/31'])
|
||||
[IPNetwork('192.0.2.112/29')]
|
||||
|
||||
>>> cidr_merge(['192.0.2.112/30', '192.0.2.116/32', '192.0.2.118/31'])
|
||||
[IPNetwork('192.0.2.112/30'), IPNetwork('192.0.2.116/32'), IPNetwork('192.0.2.118/31')]
|
||||
|
||||
>>> cidr_merge(['192.0.2.112/31', '192.0.2.116/31', '192.0.2.118/31'])
|
||||
[IPNetwork('192.0.2.112/31'), IPNetwork('192.0.2.116/30')]
|
||||
|
||||
>>> cidr_merge(['192.0.1.254/31',
|
||||
... '192.0.2.0/28',
|
||||
... '192.0.2.16/28',
|
||||
... '192.0.2.32/28',
|
||||
... '192.0.2.48/28',
|
||||
... '192.0.2.64/28',
|
||||
... '192.0.2.80/28',
|
||||
... '192.0.2.96/28',
|
||||
... '192.0.2.112/28',
|
||||
... '192.0.2.128/28',
|
||||
... '192.0.2.144/28',
|
||||
... '192.0.2.160/28',
|
||||
... '192.0.2.176/28',
|
||||
... '192.0.2.192/28',
|
||||
... '192.0.2.208/28',
|
||||
... '192.0.2.224/28',
|
||||
... '192.0.2.240/28',
|
||||
... '192.0.3.0/28'])
|
||||
[IPNetwork('192.0.1.254/31'), IPNetwork('192.0.2.0/24'), IPNetwork('192.0.3.0/28')]
|
||||
|
||||
}}}
|
||||
|
||||
Extended merge tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> import random
|
||||
|
||||
# Start with a single /23 CIDR.
|
||||
|
||||
>>> orig_cidr_ipv4 = IPNetwork('192.0.2.0/23')
|
||||
>>> orig_cidr_ipv6 = IPNetwork('::192.0.2.0/120')
|
||||
|
||||
# Split it into /28 subnet CIDRs (mix CIDR objects and CIDR strings).
|
||||
|
||||
>>> cidr_subnets = []
|
||||
>>> cidr_subnets.extend([str(c) for c in orig_cidr_ipv4.subnet(28)])
|
||||
>>> cidr_subnets.extend(list(orig_cidr_ipv4.subnet(28)))
|
||||
>>> cidr_subnets.extend([str(c) for c in orig_cidr_ipv6.subnet(124)])
|
||||
>>> cidr_subnets.extend(list(orig_cidr_ipv6.subnet(124)))
|
||||
|
||||
# Add a couple of duplicates in to make sure summarization is working OK.
|
||||
|
||||
>>> cidr_subnets.append('192.0.2.1/32')
|
||||
>>> cidr_subnets.append('192.0.2.128/25')
|
||||
>>> cidr_subnets.append('::192.0.2.92/128')
|
||||
|
||||
# Randomize the order of subnets.
|
||||
>>> random.shuffle(cidr_subnets)
|
||||
|
||||
# Perform summarization operation.
|
||||
>>> merged_cidrs = cidr_merge(cidr_subnets)
|
||||
>>> merged_cidrs
|
||||
[IPNetwork('192.0.2.0/23'), IPNetwork('::192.0.2.0/120')]
|
||||
|
||||
>>> merged_cidrs == [orig_cidr_ipv4, orig_cidr_ipv6]
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,216 @@
|
||||
=IP Constructor Stress Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress constructor - integer values.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress(1)
|
||||
IPAddress('0.0.0.1')
|
||||
|
||||
>>> IPAddress(1, 4)
|
||||
IPAddress('0.0.0.1')
|
||||
|
||||
>>> IPAddress(1, 6)
|
||||
IPAddress('::1')
|
||||
|
||||
>>> IPAddress(10)
|
||||
IPAddress('0.0.0.10')
|
||||
|
||||
>>> IPAddress(0x1ffffffff)
|
||||
IPAddress('::1:ffff:ffff')
|
||||
|
||||
>>> IPAddress(0xffffffff, 6)
|
||||
IPAddress('::255.255.255.255')
|
||||
|
||||
>>> IPAddress(0x1ffffffff)
|
||||
IPAddress('::1:ffff:ffff')
|
||||
|
||||
>>> IPAddress(2 ** 128 - 1)
|
||||
IPAddress('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress constructor - IPv4 inet_aton behaviour (default).
|
||||
|
||||
{{{
|
||||
|
||||
# Hexadecimal octets.
|
||||
>>> IPAddress('0x7f.0x1')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
>>> IPAddress('0x7f.0x0.0x0.0x1')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
# Octal octets.
|
||||
>>> IPAddress('0177.01')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
# Mixed octets.
|
||||
>>> IPAddress('0x7f.0.01')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
# Partial addresses - pretty weird ...
|
||||
>>> IPAddress('127')
|
||||
IPAddress('0.0.0.127')
|
||||
|
||||
>>> IPAddress('127')
|
||||
IPAddress('0.0.0.127')
|
||||
|
||||
>>> IPAddress('127.1')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
>>> IPAddress('127.0.1')
|
||||
IPAddress('127.0.0.1')
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress constructor - IPv4 inet_pton behaviour (stricter parser).
|
||||
|
||||
{{{
|
||||
|
||||
# Octal octets.
|
||||
>>> IPAddress('0177.01', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: failed to detect a valid IP address from '0177.01'
|
||||
|
||||
# Mixed octets.
|
||||
>>> IPAddress('0x7f.0.01', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: failed to detect a valid IP address from '0x7f.0.01'
|
||||
|
||||
# Partial octets.
|
||||
>>> IPAddress('10', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: failed to detect a valid IP address from '10'
|
||||
|
||||
>>> IPAddress('10.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: failed to detect a valid IP address from '10.1'
|
||||
|
||||
>>> IPAddress('10.0.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: failed to detect a valid IP address from '10.0.1'
|
||||
|
||||
>>> IPAddress('10.0.0.1', flags=INET_PTON)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress constructor - zero filled octets.
|
||||
|
||||
{{{
|
||||
|
||||
# This takes a lot of people by surprise ...
|
||||
>>> IPAddress('010.000.000.001')
|
||||
IPAddress('8.0.0.1')
|
||||
|
||||
# So, we need this!
|
||||
>>> IPAddress('010.000.000.001', flags=ZEROFILL)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
# Zero-fill with inet_aton behaviour - partial octets are OK but zero-filled
|
||||
# octets are interpreted as decimal ...
|
||||
>>> IPAddress('010.000.001', flags=ZEROFILL)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
# Zero-fill with inet_pton behaviour - 4 octets only!
|
||||
>>> IPAddress('010.000.001', flags=INET_PTON|ZEROFILL)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: failed to detect a valid IP address from '010.000.001'
|
||||
|
||||
# Zero-fill with inet_pton behaviour - 4 octets only!
|
||||
>>> IPAddress('010.000.000.001', flags=INET_PTON|ZEROFILL)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
# To save some typing there are short versions of these flags.
|
||||
>>> IPAddress('010.000.000.001', flags=P|Z)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
}}}
|
||||
|
||||
IP network construction.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24')
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork('192.0.2.0/255.255.255.0')
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork('192.0.2.0/0.0.0.255')
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork(IPNetwork('192.0.2.0/24'))
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork(IPNetwork('::192.0.2.0/120'))
|
||||
IPNetwork('::192.0.2.0/120')
|
||||
|
||||
>>> IPNetwork(IPNetwork('192.0.2.0/24'))
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork('::192.0.2.0/120')
|
||||
IPNetwork('::192.0.2.0/120')
|
||||
|
||||
>>> IPNetwork('::192.0.2.0/120', 6)
|
||||
IPNetwork('::192.0.2.0/120')
|
||||
|
||||
}}}
|
||||
|
||||
Optional implicit IP network prefix selection rules.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0', implicit_prefix=True)
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> IPNetwork('231.192.0.15', implicit_prefix=True)
|
||||
IPNetwork('231.192.0.15/4')
|
||||
|
||||
>>> IPNetwork('10', implicit_prefix=True)
|
||||
IPNetwork('10.0.0.0/8')
|
||||
|
||||
}}}
|
||||
|
||||
Optional flags for tweaking IPNetwork constructor behaviour.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('172.24.200')
|
||||
IPNetwork('172.24.200.0/32')
|
||||
|
||||
>>> IPNetwork('172.24.200', implicit_prefix=True)
|
||||
IPNetwork('172.24.200.0/16')
|
||||
|
||||
# Truncate the host bits so we get a pure network.
|
||||
>>> IPNetwork('172.24.200', implicit_prefix=True, flags=NOHOST)
|
||||
IPNetwork('172.24.0.0/16')
|
||||
|
||||
|
||||
}}}
|
||||
|
||||
Negative testing
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('foo')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: invalid IPNetwork foo
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,27 @@
|
||||
=IP formatting options=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
==IPAddress representations==
|
||||
|
||||
{{{
|
||||
|
||||
>>> hex(IPAddress(0))
|
||||
'0x0'
|
||||
|
||||
>>> hex(IPAddress(0xffffffff))
|
||||
'0xffffffff'
|
||||
|
||||
>>> oct(IPAddress(0))
|
||||
'0o0'
|
||||
|
||||
>>> oct(IPAddress(0xffffffff))
|
||||
'0o37777777777'
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,48 @@
|
||||
=IP Function Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
During a cidr merge operation, the address 0.0.0.0/0, representing the whole of the IPv4 address space, should swallow anything it is merged with.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(['0.0.0.0/0', '0.0.0.0'])
|
||||
[IPNetwork('0.0.0.0/0')]
|
||||
|
||||
>>> cidr_merge(['0.0.0.0/0', '255.255.255.255'])
|
||||
[IPNetwork('0.0.0.0/0')]
|
||||
|
||||
>>> cidr_merge(['0.0.0.0/0', '192.0.2.0/24', '10.0.0.0/8'])
|
||||
[IPNetwork('0.0.0.0/0')]
|
||||
|
||||
}}}
|
||||
|
||||
Same goes for the IPv6 CIDR ::/0, representing the whole of the IPv6 address space.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(['::/0', 'fe80::1'])
|
||||
[IPNetwork('::/0')]
|
||||
|
||||
>>> cidr_merge(['::/0', '::'])
|
||||
[IPNetwork('::/0')]
|
||||
|
||||
>>> cidr_merge(['::/0', '::192.0.2.0/124', 'ff00::101'])
|
||||
[IPNetwork('::/0')]
|
||||
|
||||
}}}
|
||||
|
||||
This also applies to mixed IPv4 and IPv6 address lists.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(['0.0.0.0/0', '0.0.0.0', '::/0', '::'])
|
||||
[IPNetwork('0.0.0.0/0'), IPNetwork('::/0')]
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,80 @@
|
||||
=IntSet Tests=
|
||||
|
||||
Copyright (c) 2006, Heiko Wundram.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.ip.intset import IntSet
|
||||
|
||||
>>> x = IntSet((10, 20), 30)
|
||||
>>> y = IntSet((10, 20))
|
||||
>>> z = IntSet((10, 20), 30, (15, 19), min=0, max=40)
|
||||
|
||||
>>> x
|
||||
IntSet((10,20),30)
|
||||
|
||||
>>> x & 110
|
||||
IntSet()
|
||||
|
||||
>>> x | 110
|
||||
IntSet((10,20),30,110)
|
||||
|
||||
>>> x ^ (15, 25)
|
||||
IntSet((10,14),(21,25),30)
|
||||
|
||||
>>> x - 12
|
||||
IntSet((10,11),(13,20),30)
|
||||
|
||||
>>> 12 in x
|
||||
True
|
||||
|
||||
>>> x.issubset(x)
|
||||
True
|
||||
|
||||
>>> y.issubset(x)
|
||||
True
|
||||
|
||||
>>> x.istruesubset(x)
|
||||
False
|
||||
|
||||
>>> y.istruesubset(x)
|
||||
True
|
||||
|
||||
>>> for val in x:
|
||||
... val
|
||||
10
|
||||
11
|
||||
12
|
||||
13
|
||||
14
|
||||
15
|
||||
16
|
||||
17
|
||||
18
|
||||
19
|
||||
20
|
||||
30
|
||||
|
||||
|
||||
>>> x.inverse()
|
||||
IntSet((None,9),(21,29),(31,None))
|
||||
|
||||
>>> x == z
|
||||
True
|
||||
|
||||
>>> x == y
|
||||
False
|
||||
|
||||
>>> x != y
|
||||
True
|
||||
|
||||
>>> hash(x) == hash(z)
|
||||
True
|
||||
|
||||
>>> len(x)
|
||||
12
|
||||
|
||||
>>> x.len()
|
||||
12
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,72 @@
|
||||
=IP Glob Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
IP Glob tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_to_glob('10.0.0.1/32')
|
||||
'10.0.0.1'
|
||||
|
||||
>>> cidr_to_glob('192.0.2.0/24')
|
||||
'192.0.2.*'
|
||||
|
||||
>>> cidr_to_glob('172.16.0.0/12')
|
||||
'172.16-31.*.*'
|
||||
|
||||
>>> cidr_to_glob('0.0.0.0/0')
|
||||
'*.*.*.*'
|
||||
|
||||
>>> glob_to_cidrs('10.0.0.1')
|
||||
[IPNetwork('10.0.0.1/32')]
|
||||
|
||||
>>> glob_to_cidrs('192.0.2.*')
|
||||
[IPNetwork('192.0.2.0/24')]
|
||||
|
||||
>>> glob_to_cidrs('172.16-31.*.*')
|
||||
[IPNetwork('172.16.0.0/12')]
|
||||
|
||||
>>> glob_to_cidrs('*.*.*.*')
|
||||
[IPNetwork('0.0.0.0/0')]
|
||||
|
||||
>>> glob_to_iptuple('*.*.*.*')
|
||||
(IPAddress('0.0.0.0'), IPAddress('255.255.255.255'))
|
||||
|
||||
>>> iprange_to_globs('192.0.2.0', '192.0.2.255')
|
||||
['192.0.2.*']
|
||||
|
||||
>>> iprange_to_globs('192.0.2.1', '192.0.2.15')
|
||||
['192.0.2.1-15']
|
||||
|
||||
>>> iprange_to_globs('192.0.2.255', '192.0.4.1')
|
||||
['192.0.2.255', '192.0.3.*', '192.0.4.0-1']
|
||||
|
||||
>>> iprange_to_globs('10.0.1.255', '10.0.255.255')
|
||||
['10.0.1.255', '10.0.2-3.*', '10.0.4-7.*', '10.0.8-15.*', '10.0.16-31.*', '10.0.32-63.*', '10.0.64-127.*', '10.0.128-255.*']
|
||||
|
||||
}}}
|
||||
|
||||
Validity tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> valid_glob('1.1.1.a')
|
||||
False
|
||||
|
||||
>>> valid_glob('1.1.1.1/32')
|
||||
False
|
||||
|
||||
>>> valid_glob('1.1.1.a-b')
|
||||
False
|
||||
|
||||
>>> valid_glob('1.1.a-b.*')
|
||||
False
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,159 @@
|
||||
=IPRange Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Constructor tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('192.0.2.1', '192.0.2.254')
|
||||
|
||||
>>> iprange
|
||||
IPRange('192.0.2.1', '192.0.2.254')
|
||||
|
||||
>>> '%s' % iprange
|
||||
'192.0.2.1-192.0.2.254'
|
||||
|
||||
>>> IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')
|
||||
IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')
|
||||
|
||||
>>> IPRange('192.0.2.1', '192.0.2.1')
|
||||
IPRange('192.0.2.1', '192.0.2.1')
|
||||
|
||||
>>> IPRange('208.049.164.000', '208.050.066.255', flags=ZEROFILL)
|
||||
IPRange('208.49.164.0', '208.50.66.255')
|
||||
|
||||
}}}
|
||||
|
||||
Bad constructor tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPRange('192.0.2.2', '192.0.2.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: lower bound IP greater than upper bound!
|
||||
|
||||
>>> IPRange('::', '0.0.0.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: base address '0.0.0.1' is not IPv6
|
||||
|
||||
>>> IPRange('0.0.0.0', '::1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: base address '::1' is not IPv4
|
||||
|
||||
}}}
|
||||
|
||||
Indexing and slicing tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('192.0.2.1', '192.0.2.254')
|
||||
|
||||
>>> len(iprange)
|
||||
254
|
||||
|
||||
>>> iprange.first == 3221225985
|
||||
True
|
||||
|
||||
>>> iprange.last == 3221226238
|
||||
True
|
||||
|
||||
>>> iprange[0]
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> iprange[-1]
|
||||
IPAddress('192.0.2.254')
|
||||
|
||||
>>> iprange[512]
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
IndexError: index out range for address range size!
|
||||
|
||||
>>> list(iprange[0:3])
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3')]
|
||||
|
||||
>>> list(iprange[0:10:2])
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.3'), IPAddress('192.0.2.5'), IPAddress('192.0.2.7'), IPAddress('192.0.2.9')]
|
||||
|
||||
>>> list(iprange[0:1024:512])
|
||||
[IPAddress('192.0.2.1')]
|
||||
|
||||
>>> IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')[0:10:2]
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: IPv6 slices are not supported!
|
||||
|
||||
}}}
|
||||
|
||||
Membership tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPRange('192.0.2.5', '192.0.2.10') in IPRange('192.0.2.1', '192.0.2.254')
|
||||
True
|
||||
|
||||
>>> IPRange('fe80::1', 'fe80::fffe') in IPRange('fe80::', 'fe80::ffff:ffff:ffff:ffff')
|
||||
True
|
||||
|
||||
>>> IPRange('192.0.2.5', '192.0.2.10') in IPRange('::', '::255.255.255.255')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Sorting tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ipranges = (IPRange('192.0.2.40', '192.0.2.50'), IPRange('192.0.2.20', '192.0.2.30'), IPRange('192.0.2.1', '192.0.2.254'),)
|
||||
|
||||
>>> sorted(ipranges)
|
||||
[IPRange('192.0.2.1', '192.0.2.254'), IPRange('192.0.2.20', '192.0.2.30'), IPRange('192.0.2.40', '192.0.2.50')]
|
||||
|
||||
>>> ipranges = list(ipranges)
|
||||
|
||||
>>> ipranges.append(IPRange('192.0.2.45', '192.0.2.49'))
|
||||
|
||||
>>> sorted(ipranges)
|
||||
[IPRange('192.0.2.1', '192.0.2.254'), IPRange('192.0.2.20', '192.0.2.30'), IPRange('192.0.2.40', '192.0.2.50'), IPRange('192.0.2.45', '192.0.2.49')]
|
||||
|
||||
}}}
|
||||
|
||||
CIDR interoperability tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPRange('192.0.2.5', '192.0.2.10').cidrs()
|
||||
[IPNetwork('192.0.2.5/32'), IPNetwork('192.0.2.6/31'), IPNetwork('192.0.2.8/31'), IPNetwork('192.0.2.10/32')]
|
||||
|
||||
>>> IPRange('fe80::', 'fe80::ffff:ffff:ffff:ffff').cidrs()
|
||||
[IPNetwork('fe80::/64')]
|
||||
|
||||
}}}
|
||||
|
||||
Various additional tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange.info
|
||||
{'IPv4': [{'date': '1993-05',
|
||||
'designation': 'Administered by ARIN',
|
||||
'prefix': '192/8',
|
||||
'status': 'Legacy',
|
||||
'whois': 'whois.arin.net'}]}
|
||||
|
||||
>>> iprange.is_private()
|
||||
True
|
||||
|
||||
>>> iprange.version
|
||||
4
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,65 @@
|
||||
=IP Matching Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', ['192.0.2.0'])
|
||||
IPNetwork('192.0.2.0/32')
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0'])
|
||||
IPNetwork('192.0.2.0/32')
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0', '224.0.0.1'])
|
||||
IPNetwork('192.0.2.0/32')
|
||||
|
||||
>>> smallest_matching_cidr('192.0.2.0', ['10.0.0.1', '192.0.2.0', '224.0.0.1'])
|
||||
IPNetwork('192.0.2.0/32')
|
||||
|
||||
>>> smallest_matching_cidr('192.0.2.0', ['10.0.0.1', '224.0.0.1'])
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', ['10.0.0.1', '224.0.0.1'])
|
||||
|
||||
>>> networks = [str(c) for c in IPNetwork('192.0.2.128/27').supernet(22)]
|
||||
|
||||
>>> networks
|
||||
['192.0.0.0/22', '192.0.2.0/23', '192.0.2.0/24', '192.0.2.128/25', '192.0.2.128/26']
|
||||
|
||||
>>> all_matching_cidrs('192.0.2.0', networks)
|
||||
[IPNetwork('192.0.0.0/22'), IPNetwork('192.0.2.0/23'), IPNetwork('192.0.2.0/24')]
|
||||
|
||||
>>> smallest_matching_cidr('192.0.2.0', networks)
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> largest_matching_cidr('192.0.2.0', networks)
|
||||
IPNetwork('192.0.0.0/22')
|
||||
|
||||
}}}
|
||||
|
||||
Checking matches with varying IP address versions.
|
||||
|
||||
{{{
|
||||
>>> all_matching_cidrs('192.0.2.0', ['192.0.2.0/24'])
|
||||
[IPNetwork('192.0.2.0/24')]
|
||||
|
||||
>>> all_matching_cidrs('192.0.2.0', ['::/96'])
|
||||
[]
|
||||
|
||||
>>> all_matching_cidrs('::ffff:192.0.2.1', ['::ffff:192.0.2.0/96'])
|
||||
[IPNetwork('::ffff:192.0.2.0/96')]
|
||||
|
||||
>>> all_matching_cidrs('::192.0.2.1', ['::192.0.2.0/96'])
|
||||
[IPNetwork('::192.0.2.0/96')]
|
||||
|
||||
>>> all_matching_cidrs('::192.0.2.1', ['192.0.2.0/23'])
|
||||
[]
|
||||
|
||||
>>> all_matching_cidrs('::192.0.2.1', ['192.0.2.0/24', '::192.0.2.0/120'])
|
||||
[IPNetwork('::192.0.2.0/120')]
|
||||
|
||||
>>> all_matching_cidrs('::192.0.2.1', [IPNetwork('192.0.2.0/24'), IPNetwork('::192.0.2.0/120')])
|
||||
[IPNetwork('::192.0.2.0/120')]
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,30 @@
|
||||
=IP Multicast Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
>>> ip = IPAddress('239.192.0.1')
|
||||
|
||||
>>> ip.is_multicast()
|
||||
True
|
||||
|
||||
>>> ip = IPAddress(3221225984)
|
||||
|
||||
>>> ip = IPAddress('224.0.1.173')
|
||||
|
||||
>>> ip.info.IPv4[0].designation
|
||||
'Multicast'
|
||||
|
||||
>>> ip.info.IPv4[0].prefix
|
||||
'224/8'
|
||||
|
||||
>>> ip.info.IPv4[0].status
|
||||
'Reserved'
|
||||
|
||||
>>> ip.info.Multicast[0].address
|
||||
'224.0.1.173'
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,114 @@
|
||||
=nmap IP Range Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
nmap IP range validation.
|
||||
|
||||
{{{
|
||||
|
||||
>>> valid_nmap_range('192.0.2.1')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('192.0.2.0-31')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('192.0.2-3.1-254')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.0-255')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('192.168.3-5,7.1')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range('192.168.3-5,7,10-12,13,14.1')
|
||||
True
|
||||
|
||||
>>> valid_nmap_range(1)
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('1')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range([])
|
||||
False
|
||||
|
||||
>>> valid_nmap_range({})
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('::')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('255.255.255.256')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.0-256')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.-1-0')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.256-0')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('0-255.0-255.0-255.255-0')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('a.b.c.d-e')
|
||||
False
|
||||
|
||||
>>> valid_nmap_range('255.255.255.a-b')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
nmap IP range iteration.
|
||||
|
||||
{{{
|
||||
|
||||
>>> list(iter_nmap_range('192.0.2.1'))
|
||||
[IPAddress('192.0.2.1')]
|
||||
|
||||
>>> ip_list = list(iter_nmap_range('192.0.2.0-31'))
|
||||
>>> len(ip_list)
|
||||
32
|
||||
>>> ip_list
|
||||
[IPAddress('192.0.2.0'), IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15'), IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), IPAddress('192.0.2.18'), IPAddress('192.0.2.19'), IPAddress('192.0.2.20'), IPAddress('192.0.2.21'), IPAddress('192.0.2.22'), IPAddress('192.0.2.23'), IPAddress('192.0.2.24'), IPAddress('192.0.2.25'), IPAddress('192.0.2.26'), IPAddress('192.0.2.27'), IPAddress('192.0.2.28'), IPAddress('192.0.2.29'), IPAddress('192.0.2.30'), IPAddress('192.0.2.31')]
|
||||
|
||||
>>> ip_list = list(iter_nmap_range('192.0.2-3.1-7'))
|
||||
>>> len(ip_list)
|
||||
14
|
||||
|
||||
>>> list(iter_nmap_range('192.0.2.1-3,5,7-9'))
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.5'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9')]
|
||||
|
||||
>>> for ip in ip_list:
|
||||
... print(ip)
|
||||
...
|
||||
192.0.2.1
|
||||
192.0.2.2
|
||||
192.0.2.3
|
||||
192.0.2.4
|
||||
192.0.2.5
|
||||
192.0.2.6
|
||||
192.0.2.7
|
||||
192.0.3.1
|
||||
192.0.3.2
|
||||
192.0.3.3
|
||||
192.0.3.4
|
||||
192.0.3.5
|
||||
192.0.3.6
|
||||
192.0.3.7
|
||||
|
||||
>>> list(iter_nmap_range('::'))
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: invalid nmap range: ::
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,214 @@
|
||||
=IP Persistence Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
>>> import pickle
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress object pickling - IPv4.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress(3221225985)
|
||||
>>> ip
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> buf = pickle.dumps(ip)
|
||||
|
||||
>>> ip2 = pickle.loads(buf)
|
||||
|
||||
>>> ip2 == ip
|
||||
True
|
||||
|
||||
>>> id(ip2) != id(ip)
|
||||
True
|
||||
|
||||
>>> ip2.value == 3221225985
|
||||
True
|
||||
|
||||
>>> ip2.version
|
||||
4
|
||||
|
||||
>>> del ip, buf, ip2
|
||||
|
||||
}}}
|
||||
|
||||
IPAddress object pickling - IPv6.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress('::ffff:192.0.2.1')
|
||||
|
||||
>>> ip
|
||||
IPAddress('::ffff:192.0.2.1')
|
||||
|
||||
>>> ip.value == 281473902969345
|
||||
True
|
||||
|
||||
>>> buf = pickle.dumps(ip)
|
||||
|
||||
>>> ip2 = pickle.loads(buf)
|
||||
|
||||
>>> ip2 == ip
|
||||
True
|
||||
|
||||
>>> ip2.value == 281473902969345
|
||||
True
|
||||
|
||||
>>> ip2.version
|
||||
6
|
||||
|
||||
>>> del ip, buf, ip2
|
||||
|
||||
}}}
|
||||
|
||||
IPNetwork pickling - IPv4.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr = IPNetwork('192.0.2.0/24')
|
||||
>>> cidr
|
||||
IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> buf = pickle.dumps(cidr)
|
||||
|
||||
>>> cidr2 = pickle.loads(buf)
|
||||
|
||||
>>> cidr2 == cidr
|
||||
True
|
||||
|
||||
>>> id(cidr2) != id(cidr)
|
||||
True
|
||||
|
||||
>>> cidr2.value == 3221225984
|
||||
True
|
||||
|
||||
>>> cidr2.prefixlen
|
||||
24
|
||||
|
||||
>>> cidr2.version
|
||||
4
|
||||
|
||||
>>> del cidr, buf, cidr2
|
||||
|
||||
}}}
|
||||
|
||||
IPNetwork object pickling - IPv6.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr = IPNetwork('::ffff:192.0.2.0/120')
|
||||
|
||||
>>> cidr
|
||||
IPNetwork('::ffff:192.0.2.0/120')
|
||||
|
||||
>>> cidr.value == 281473902969344
|
||||
True
|
||||
|
||||
>>> cidr.prefixlen
|
||||
120
|
||||
|
||||
>>> buf = pickle.dumps(cidr)
|
||||
|
||||
>>> cidr2 = pickle.loads(buf)
|
||||
|
||||
>>> cidr2 == cidr
|
||||
True
|
||||
|
||||
>>> cidr2.value == 281473902969344
|
||||
True
|
||||
|
||||
>>> cidr2.prefixlen
|
||||
120
|
||||
|
||||
>>> cidr2.version
|
||||
6
|
||||
|
||||
>>> del cidr, buf, cidr2
|
||||
|
||||
}}}
|
||||
|
||||
}}}
|
||||
|
||||
IPRange object pickling - IPv4.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('192.0.2.1', '192.0.2.254')
|
||||
>>> iprange
|
||||
IPRange('192.0.2.1', '192.0.2.254')
|
||||
|
||||
>>> iprange.first == 3221225985
|
||||
True
|
||||
|
||||
>>> iprange.last == 3221226238
|
||||
True
|
||||
|
||||
>>> iprange.version
|
||||
4
|
||||
|
||||
>>> buf = pickle.dumps(iprange)
|
||||
|
||||
>>> iprange2 = pickle.loads(buf)
|
||||
|
||||
>>> iprange2 == iprange
|
||||
True
|
||||
|
||||
>>> id(iprange2) != id(iprange)
|
||||
True
|
||||
|
||||
>>> iprange2.first == 3221225985
|
||||
True
|
||||
|
||||
>>> iprange2.last == 3221226238
|
||||
True
|
||||
|
||||
>>> iprange2.version
|
||||
4
|
||||
|
||||
>>> del iprange, buf, iprange2
|
||||
|
||||
}}}
|
||||
|
||||
IPRange object pickling - IPv6.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')
|
||||
|
||||
>>> iprange
|
||||
IPRange('::ffff:192.0.2.1', '::ffff:192.0.2.254')
|
||||
|
||||
>>> iprange.first == 281473902969345
|
||||
True
|
||||
|
||||
>>> iprange.last == 281473902969598
|
||||
True
|
||||
|
||||
>>> iprange.version
|
||||
6
|
||||
|
||||
>>> buf = pickle.dumps(iprange)
|
||||
|
||||
>>> iprange2 = pickle.loads(buf)
|
||||
|
||||
>>> iprange2 == iprange
|
||||
True
|
||||
|
||||
>>> iprange2.first == 281473902969345
|
||||
True
|
||||
|
||||
>>> iprange2.last == 281473902969598
|
||||
True
|
||||
|
||||
>>> iprange2.version
|
||||
6
|
||||
|
||||
>>> del iprange, buf, iprange2
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
=Mac OSX Specific Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 compatible IPv6 range to CIDR.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('::1', '::255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
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')
|
||||
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')
|
||||
IPNetwork('::240.0.0.0/101')
|
||||
IPNetwork('::248.0.0.0/102')
|
||||
IPNetwork('::252.0.0.0/103')
|
||||
IPNetwork('::254.0.0.0/104')
|
||||
IPNetwork('::255.0.0.0/105')
|
||||
IPNetwork('::255.128.0.0/106')
|
||||
IPNetwork('::255.192.0.0/107')
|
||||
IPNetwork('::255.224.0.0/108')
|
||||
IPNetwork('::255.240.0.0/109')
|
||||
IPNetwork('::255.248.0.0/110')
|
||||
IPNetwork('::255.252.0.0/111')
|
||||
IPNetwork('::255.254.0.0/112')
|
||||
IPNetwork('::255.255.0.0/113')
|
||||
IPNetwork('::255.255.128.0/114')
|
||||
IPNetwork('::255.255.192.0/115')
|
||||
IPNetwork('::255.255.224.0/116')
|
||||
IPNetwork('::255.255.240.0/117')
|
||||
IPNetwork('::255.255.248.0/118')
|
||||
IPNetwork('::255.255.252.0/119')
|
||||
IPNetwork('::255.255.254.0/120')
|
||||
IPNetwork('::255.255.255.0/121')
|
||||
IPNetwork('::255.255.255.128/122')
|
||||
IPNetwork('::255.255.255.192/123')
|
||||
IPNetwork('::255.255.255.224/124')
|
||||
IPNetwork('::255.255.255.240/125')
|
||||
IPNetwork('::255.255.255.248/126')
|
||||
IPNetwork('::255.255.255.252/127')
|
||||
IPNetwork('::255.255.255.254/128')
|
||||
|
||||
# inet_pton has to be different on Mac OSX *sigh*
|
||||
>>> IPAddress('010.000.000.001', flags=INET_PTON)
|
||||
IPAddress('10.0.0.1')
|
||||
|
||||
>>> from netaddr.strategy.ipv6 import int_to_str
|
||||
>>> int_to_str(0xffff)
|
||||
'::0.0.255.255'
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
=Linux Specific Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 compatible IPv6 range to CIDR.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('::1', '::255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
IPNetwork('::1/128')
|
||||
IPNetwork('::2/127')
|
||||
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')
|
||||
IPNetwork('::240.0.0.0/101')
|
||||
IPNetwork('::248.0.0.0/102')
|
||||
IPNetwork('::252.0.0.0/103')
|
||||
IPNetwork('::254.0.0.0/104')
|
||||
IPNetwork('::255.0.0.0/105')
|
||||
IPNetwork('::255.128.0.0/106')
|
||||
IPNetwork('::255.192.0.0/107')
|
||||
IPNetwork('::255.224.0.0/108')
|
||||
IPNetwork('::255.240.0.0/109')
|
||||
IPNetwork('::255.248.0.0/110')
|
||||
IPNetwork('::255.252.0.0/111')
|
||||
IPNetwork('::255.254.0.0/112')
|
||||
IPNetwork('::255.255.0.0/113')
|
||||
IPNetwork('::255.255.128.0/114')
|
||||
IPNetwork('::255.255.192.0/115')
|
||||
IPNetwork('::255.255.224.0/116')
|
||||
IPNetwork('::255.255.240.0/117')
|
||||
IPNetwork('::255.255.248.0/118')
|
||||
IPNetwork('::255.255.252.0/119')
|
||||
IPNetwork('::255.255.254.0/120')
|
||||
IPNetwork('::255.255.255.0/121')
|
||||
IPNetwork('::255.255.255.128/122')
|
||||
IPNetwork('::255.255.255.192/123')
|
||||
IPNetwork('::255.255.255.224/124')
|
||||
IPNetwork('::255.255.255.240/125')
|
||||
IPNetwork('::255.255.255.248/126')
|
||||
IPNetwork('::255.255.255.252/127')
|
||||
IPNetwork('::255.255.255.254/128')
|
||||
|
||||
# Sadly, inet_pton cannot help us here ...
|
||||
>>> IPAddress('010.000.000.001', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: failed to detect a valid IP address from '010.000.000.001'
|
||||
|
||||
>>> from netaddr.strategy.ipv6 import int_to_str
|
||||
>>> int_to_str(0xffff)
|
||||
'::ffff'
|
||||
|
||||
}}}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
=Windows Specific Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Worst case IPv4 compatible IPv6 range to CIDR.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in iprange_to_cidrs('::1', '::255.255.255.254'):
|
||||
... ip
|
||||
...
|
||||
IPNetwork('::1/128')
|
||||
IPNetwork('::2/127')
|
||||
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')
|
||||
IPNetwork('::240.0.0.0/101')
|
||||
IPNetwork('::248.0.0.0/102')
|
||||
IPNetwork('::252.0.0.0/103')
|
||||
IPNetwork('::254.0.0.0/104')
|
||||
IPNetwork('::255.0.0.0/105')
|
||||
IPNetwork('::255.128.0.0/106')
|
||||
IPNetwork('::255.192.0.0/107')
|
||||
IPNetwork('::255.224.0.0/108')
|
||||
IPNetwork('::255.240.0.0/109')
|
||||
IPNetwork('::255.248.0.0/110')
|
||||
IPNetwork('::255.252.0.0/111')
|
||||
IPNetwork('::255.254.0.0/112')
|
||||
IPNetwork('::255.255.0.0/113')
|
||||
IPNetwork('::255.255.128.0/114')
|
||||
IPNetwork('::255.255.192.0/115')
|
||||
IPNetwork('::255.255.224.0/116')
|
||||
IPNetwork('::255.255.240.0/117')
|
||||
IPNetwork('::255.255.248.0/118')
|
||||
IPNetwork('::255.255.252.0/119')
|
||||
IPNetwork('::255.255.254.0/120')
|
||||
IPNetwork('::255.255.255.0/121')
|
||||
IPNetwork('::255.255.255.128/122')
|
||||
IPNetwork('::255.255.255.192/123')
|
||||
IPNetwork('::255.255.255.224/124')
|
||||
IPNetwork('::255.255.255.240/125')
|
||||
IPNetwork('::255.255.255.248/126')
|
||||
IPNetwork('::255.255.255.252/127')
|
||||
IPNetwork('::255.255.255.254/128')
|
||||
|
||||
# Sadly, inet_pton cannot help us here ...
|
||||
>>> IPAddress('010.000.000.001', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: failed to detect a valid IP address from '010.000.000.001'
|
||||
|
||||
>>> from netaddr.strategy.ipv6 import int_to_str
|
||||
>>> int_to_str(0xffff)
|
||||
'::ffff'
|
||||
|
||||
}}}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
=RFC 1924 Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
The example from the RFC.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.ip.rfc1924 import ipv6_to_base85, base85_to_ipv6
|
||||
|
||||
>>> ip_addr = '1080::8:800:200c:417a'
|
||||
|
||||
>>> ip_addr
|
||||
'1080::8:800:200c:417a'
|
||||
|
||||
>>> base85 = ipv6_to_base85(ip_addr)
|
||||
|
||||
>>> base85
|
||||
'4)+k&C#VzJ4br>0wv%Yp'
|
||||
|
||||
>>> base85_to_ipv6(base85)
|
||||
'1080::8:800:200c:417a'
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,447 @@
|
||||
=IPSet Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Basic operations.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPSet()
|
||||
IPSet([])
|
||||
|
||||
>>> IPSet([])
|
||||
IPSet([])
|
||||
|
||||
>>> IPSet(['192.0.2.0'])
|
||||
IPSet(['192.0.2.0/32'])
|
||||
|
||||
>>> IPSet([IPAddress('192.0.2.0')])
|
||||
IPSet(['192.0.2.0/32'])
|
||||
|
||||
>>> IPSet([IPNetwork('192.0.2.0')])
|
||||
IPSet(['192.0.2.0/32'])
|
||||
|
||||
>>> IPSet([IPNetwork('192.0.2.0/24')])
|
||||
IPSet(['192.0.2.0/24'])
|
||||
|
||||
>>> for ip in IPSet(['192.0.2.0/28', '::192.0.2.0/124']):
|
||||
... print(ip)
|
||||
192.0.2.0
|
||||
192.0.2.1
|
||||
192.0.2.2
|
||||
192.0.2.3
|
||||
192.0.2.4
|
||||
192.0.2.5
|
||||
192.0.2.6
|
||||
192.0.2.7
|
||||
192.0.2.8
|
||||
192.0.2.9
|
||||
192.0.2.10
|
||||
192.0.2.11
|
||||
192.0.2.12
|
||||
192.0.2.13
|
||||
192.0.2.14
|
||||
192.0.2.15
|
||||
::192.0.2.0
|
||||
::192.0.2.1
|
||||
::192.0.2.2
|
||||
::192.0.2.3
|
||||
::192.0.2.4
|
||||
::192.0.2.5
|
||||
::192.0.2.6
|
||||
::192.0.2.7
|
||||
::192.0.2.8
|
||||
::192.0.2.9
|
||||
::192.0.2.10
|
||||
::192.0.2.11
|
||||
::192.0.2.12
|
||||
::192.0.2.13
|
||||
::192.0.2.14
|
||||
::192.0.2.15
|
||||
|
||||
}}}
|
||||
|
||||
Adding and removing elements.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 = IPSet()
|
||||
|
||||
>>> s1.add('192.0.2.0')
|
||||
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/32'])
|
||||
|
||||
>>> s1.remove('192.0.2.0')
|
||||
|
||||
>>> s1
|
||||
IPSet([])
|
||||
|
||||
>>> s1.remove('192.0.2.0')
|
||||
|
||||
}}}
|
||||
|
||||
Set membership.
|
||||
|
||||
{{{
|
||||
|
||||
>>> iprange = IPRange('192.0.1.255', '192.0.2.16')
|
||||
|
||||
>>> iprange.cidrs()
|
||||
[IPNetwork('192.0.1.255/32'), IPNetwork('192.0.2.0/28'), IPNetwork('192.0.2.16/32')]
|
||||
|
||||
>>> ipset = IPSet(['192.0.2.0/28'])
|
||||
|
||||
>>> for ip in iprange:
|
||||
... print(ip, ip in ipset)
|
||||
192.0.1.255 False
|
||||
192.0.2.0 True
|
||||
192.0.2.1 True
|
||||
192.0.2.2 True
|
||||
192.0.2.3 True
|
||||
192.0.2.4 True
|
||||
192.0.2.5 True
|
||||
192.0.2.6 True
|
||||
192.0.2.7 True
|
||||
192.0.2.8 True
|
||||
192.0.2.9 True
|
||||
192.0.2.10 True
|
||||
192.0.2.11 True
|
||||
192.0.2.12 True
|
||||
192.0.2.13 True
|
||||
192.0.2.14 True
|
||||
192.0.2.15 True
|
||||
192.0.2.16 False
|
||||
|
||||
}}}
|
||||
|
||||
Set union.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPSet(['192.0.2.0'])
|
||||
IPSet(['192.0.2.0/32'])
|
||||
|
||||
>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1'])
|
||||
IPSet(['192.0.2.0/31'])
|
||||
|
||||
>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3'])
|
||||
IPSet(['192.0.2.0/31', '192.0.2.3/32'])
|
||||
|
||||
>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3/30'])
|
||||
IPSet(['192.0.2.0/30'])
|
||||
|
||||
>>> IPSet(['192.0.2.0']) | IPSet(['192.0.2.1']) | IPSet(['192.0.2.3/31'])
|
||||
IPSet(['192.0.2.0/30'])
|
||||
|
||||
>>> IPSet(['192.0.2.0/24']) | IPSet(['192.0.3.0/24']) | IPSet(['192.0.4.0/24'])
|
||||
IPSet(['192.0.2.0/23', '192.0.4.0/24'])
|
||||
|
||||
}}}
|
||||
|
||||
A joined up example of the union, intersection and symmetric difference operations.
|
||||
|
||||
{{{
|
||||
|
||||
>>> adj_cidrs = list(IPNetwork('192.0.2.0/24').subnet(28))
|
||||
>>> even_cidrs = adj_cidrs[::2]
|
||||
|
||||
>>> evens = IPSet(even_cidrs)
|
||||
>>> evens
|
||||
IPSet(['192.0.2.0/28', '192.0.2.32/28', '192.0.2.64/28', '192.0.2.96/28', '192.0.2.128/28', '192.0.2.160/28', '192.0.2.192/28', '192.0.2.224/28'])
|
||||
|
||||
>>> IPSet(['192.0.2.0/24']) & evens
|
||||
IPSet(['192.0.2.0/28', '192.0.2.32/28', '192.0.2.64/28', '192.0.2.96/28', '192.0.2.128/28', '192.0.2.160/28', '192.0.2.192/28', '192.0.2.224/28'])
|
||||
|
||||
>>> odds = IPSet(['192.0.2.0/24']) ^ evens
|
||||
>>> odds
|
||||
IPSet(['192.0.2.16/28', '192.0.2.48/28', '192.0.2.80/28', '192.0.2.112/28', '192.0.2.144/28', '192.0.2.176/28', '192.0.2.208/28', '192.0.2.240/28'])
|
||||
|
||||
>>> evens | odds
|
||||
IPSet(['192.0.2.0/24'])
|
||||
|
||||
>>> evens & odds
|
||||
IPSet([])
|
||||
|
||||
>>> evens ^ odds
|
||||
IPSet(['192.0.2.0/24'])
|
||||
|
||||
}}}
|
||||
|
||||
Superset and subset tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0/24', '192.0.4.0/24'])
|
||||
>>> s2 = IPSet(['192.0.2.0', '192.0.4.0'])
|
||||
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/24', '192.0.4.0/24'])
|
||||
|
||||
>>> s2
|
||||
IPSet(['192.0.2.0/32', '192.0.4.0/32'])
|
||||
|
||||
>>> s1.issuperset(s2)
|
||||
True
|
||||
|
||||
>>> s2.issubset(s1)
|
||||
True
|
||||
|
||||
>>> s2.issuperset(s1)
|
||||
False
|
||||
|
||||
>>> s1.issubset(s2)
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
|
||||
|
||||
{{{
|
||||
|
||||
>>> 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
|
||||
|
||||
>>> for cidr in available.iter_cidrs():
|
||||
... print(cidr, cidr[0], cidr[-1])
|
||||
0.0.0.0/5 0.0.0.0 7.255.255.255
|
||||
8.0.0.0/7 8.0.0.0 9.255.255.255
|
||||
11.0.0.0/8 11.0.0.0 11.255.255.255
|
||||
12.0.0.0/6 12.0.0.0 15.255.255.255
|
||||
16.0.0.0/4 16.0.0.0 31.255.255.255
|
||||
32.0.0.0/3 32.0.0.0 63.255.255.255
|
||||
64.0.0.0/2 64.0.0.0 127.255.255.255
|
||||
128.0.0.0/3 128.0.0.0 159.255.255.255
|
||||
160.0.0.0/5 160.0.0.0 167.255.255.255
|
||||
168.0.0.0/6 168.0.0.0 171.255.255.255
|
||||
172.0.0.0/12 172.0.0.0 172.15.255.255
|
||||
172.32.0.0/11 172.32.0.0 172.63.255.255
|
||||
172.64.0.0/10 172.64.0.0 172.127.255.255
|
||||
172.128.0.0/9 172.128.0.0 172.255.255.255
|
||||
173.0.0.0/8 173.0.0.0 173.255.255.255
|
||||
174.0.0.0/7 174.0.0.0 175.255.255.255
|
||||
176.0.0.0/4 176.0.0.0 191.255.255.255
|
||||
192.0.0.0/23 192.0.0.0 192.0.1.255
|
||||
192.0.3.0/24 192.0.3.0 192.0.3.255
|
||||
192.0.4.0/22 192.0.4.0 192.0.7.255
|
||||
192.0.8.0/21 192.0.8.0 192.0.15.255
|
||||
192.0.16.0/20 192.0.16.0 192.0.31.255
|
||||
192.0.32.0/19 192.0.32.0 192.0.63.255
|
||||
192.0.64.0/18 192.0.64.0 192.0.127.255
|
||||
192.0.128.0/17 192.0.128.0 192.0.255.255
|
||||
192.1.0.0/16 192.1.0.0 192.1.255.255
|
||||
192.2.0.0/15 192.2.0.0 192.3.255.255
|
||||
192.4.0.0/14 192.4.0.0 192.7.255.255
|
||||
192.8.0.0/13 192.8.0.0 192.15.255.255
|
||||
192.16.0.0/12 192.16.0.0 192.31.255.255
|
||||
192.32.0.0/11 192.32.0.0 192.63.255.255
|
||||
192.64.0.0/10 192.64.0.0 192.127.255.255
|
||||
192.128.0.0/11 192.128.0.0 192.159.255.255
|
||||
192.160.0.0/13 192.160.0.0 192.167.255.255
|
||||
192.169.0.0/16 192.169.0.0 192.169.255.255
|
||||
192.170.0.0/15 192.170.0.0 192.171.255.255
|
||||
192.172.0.0/14 192.172.0.0 192.175.255.255
|
||||
192.176.0.0/12 192.176.0.0 192.191.255.255
|
||||
192.192.0.0/10 192.192.0.0 192.255.255.255
|
||||
193.0.0.0/8 193.0.0.0 193.255.255.255
|
||||
194.0.0.0/7 194.0.0.0 195.255.255.255
|
||||
196.0.0.0/6 196.0.0.0 199.255.255.255
|
||||
200.0.0.0/5 200.0.0.0 207.255.255.255
|
||||
208.0.0.0/4 208.0.0.0 223.255.255.255
|
||||
224.0.0.0/8 224.0.0.0 224.255.255.255
|
||||
232.0.0.0/7 232.0.0.0 233.255.255.255
|
||||
239.0.0.0/9 239.0.0.0 239.127.255.255
|
||||
239.128.0.0/10 239.128.0.0 239.191.255.255
|
||||
239.196.0.0/14 239.196.0.0 239.199.255.255
|
||||
239.200.0.0/13 239.200.0.0 239.207.255.255
|
||||
239.208.0.0/12 239.208.0.0 239.223.255.255
|
||||
239.224.0.0/11 239.224.0.0 239.255.255.255
|
||||
|
||||
>>> ipv4_addr_space ^ available
|
||||
IPSet(['10.0.0.0/8', '172.16.0.0/12', '192.0.2.0/24', '192.168.0.0/16', '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', '239.192.0.0/14', '240.0.0.0/4'])
|
||||
|
||||
}}}
|
||||
|
||||
==Tests on combined IPv4 and IPv6 sets==
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0', '::192.0.2.0', '192.0.2.2', '::192.0.2.2'])
|
||||
>>> s2 = IPSet(['192.0.2.2', '::192.0.2.2', '192.0.2.4', '::192.0.2.4'])
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/32', '192.0.2.2/32', '::192.0.2.0/128', '::192.0.2.2/128'])
|
||||
|
||||
>>> s2
|
||||
IPSet(['192.0.2.2/32', '192.0.2.4/32', '::192.0.2.2/128', '::192.0.2.4/128'])
|
||||
|
||||
}}}
|
||||
|
||||
Set union.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 | s2
|
||||
IPSet(['192.0.2.0/32', '192.0.2.2/32', '192.0.2.4/32', '::192.0.2.0/128', '::192.0.2.2/128', '::192.0.2.4/128'])
|
||||
|
||||
}}}
|
||||
|
||||
Set intersection.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 & s2
|
||||
IPSet(['192.0.2.2/32', '::192.0.2.2/128'])
|
||||
|
||||
}}}
|
||||
|
||||
Set difference.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 - s2
|
||||
IPSet(['192.0.2.0/32', '::192.0.2.0/128'])
|
||||
|
||||
>>> s2 - s1
|
||||
IPSet(['192.0.2.4/32', '::192.0.2.4/128'])
|
||||
|
||||
}}}
|
||||
|
||||
Symmetric set difference.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 ^ s2
|
||||
IPSet(['192.0.2.0/32', '192.0.2.4/32', '::192.0.2.0/128', '::192.0.2.4/128'])
|
||||
|
||||
}}}
|
||||
|
||||
Disjointed sets.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0', '192.0.2.1', '192.0.2.2'])
|
||||
|
||||
>>> s2 = IPSet(['192.0.2.2', '192.0.2.3', '192.0.2.4'])
|
||||
|
||||
>>> s1 & s2
|
||||
IPSet(['192.0.2.2/32'])
|
||||
|
||||
>>> s1.isdisjoint(s2)
|
||||
False
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0', '192.0.2.1'])
|
||||
|
||||
>>> s2 = IPSet(['192.0.2.3', '192.0.2.4'])
|
||||
|
||||
>>> s1 & s2
|
||||
IPSet([])
|
||||
|
||||
>>> s1.isdisjoint(s2)
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Updating a set.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 = IPSet(['192.0.2.0/25'])
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/25'])
|
||||
|
||||
>>> s2 = IPSet(['192.0.2.128/25'])
|
||||
>>> s2
|
||||
IPSet(['192.0.2.128/25'])
|
||||
|
||||
>>> s1.update(s2)
|
||||
|
||||
>>> s1
|
||||
IPSet(['192.0.2.0/24'])
|
||||
|
||||
>>> s1.update(['192.0.0.0/24', '192.0.1.0/24', '192.0.3.0/24'])
|
||||
>>> s1
|
||||
IPSet(['192.0.0.0/22'])
|
||||
|
||||
|
||||
}}}
|
||||
|
||||
Removing IP addresses from an IPSet.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1 = IPSet(['0.0.0.0/0'])
|
||||
|
||||
>>> s1
|
||||
IPSet(['0.0.0.0/0'])
|
||||
|
||||
>>> s1.remove('255.255.255.255')
|
||||
|
||||
>>> s1
|
||||
IPSet(['0.0.0.0/1', '128.0.0.0/2', ..., '255.255.255.252/31', '255.255.255.254/32'])
|
||||
|
||||
>>> list(s1.iter_cidrs())
|
||||
[IPNetwork('0.0.0.0/1'), IPNetwork('128.0.0.0/2'), ..., IPNetwork('255.255.255.252/31'), IPNetwork('255.255.255.254/32')]
|
||||
|
||||
>>> len(list(s1.iter_cidrs()))
|
||||
32
|
||||
|
||||
>>> list(s1.iter_cidrs()) == cidr_exclude('0.0.0.0/0', '255.255.255.255')
|
||||
True
|
||||
|
||||
>>> s1.remove('0.0.0.0')
|
||||
|
||||
>>> s1
|
||||
IPSet(['0.0.0.1/32', '0.0.0.2/31', ..., '255.255.255.252/31', '255.255.255.254/32'])
|
||||
|
||||
>>> len(list(s1.iter_cidrs()))
|
||||
62
|
||||
|
||||
}}}
|
||||
|
||||
Adding IP address to an IPSet.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s1.add('255.255.255.255')
|
||||
|
||||
>>> s1
|
||||
IPSet(['0.0.0.1/32', '0.0.0.2/31', ..., '64.0.0.0/2', '128.0.0.0/1'])
|
||||
|
||||
>>> list(s1.iter_cidrs())
|
||||
[IPNetwork('0.0.0.1/32'), IPNetwork('0.0.0.2/31'), ..., IPNetwork('64.0.0.0/2'), IPNetwork('128.0.0.0/1')]
|
||||
|
||||
>>> len(list(s1.iter_cidrs()))
|
||||
32
|
||||
|
||||
>>> s1.add('0.0.0.0')
|
||||
|
||||
>>> s1
|
||||
IPSet(['0.0.0.0/0'])
|
||||
|
||||
}}}
|
||||
|
||||
Pickling of IPSet objects
|
||||
|
||||
{{{
|
||||
|
||||
>>> import pickle
|
||||
>>> ip_data = IPSet(['10.0.0.0/16', 'fe80::/64'])
|
||||
>>> buf = pickle.dumps(ip_data)
|
||||
>>> ip_data_unpickled = pickle.loads(buf)
|
||||
>>> ip_data == ip_data_unpickled
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,94 @@
|
||||
=Socket Fallback Module Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.fbsocket import *
|
||||
|
||||
}}}
|
||||
|
||||
IPv6 '::' compression algorithm tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:0:0:0:0:0:0:0'))
|
||||
'::'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:0:0:0:0:0:0:A'))
|
||||
'::a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:0:0:0:0:0:0'))
|
||||
'a::'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:A:0:0:0:0:0'))
|
||||
'a:0:a::'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:0:0:0:0:0:A'))
|
||||
'a::a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:A:0:0:0:0:0:A'))
|
||||
'0:a::a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:A:0:0:0:0:A'))
|
||||
'a:0:a::a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:0:0:A:0:0:0:A'))
|
||||
'::a:0:0:0:a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:0:0:0:A:0:0:A'))
|
||||
'::a:0:0:a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:0:0:0:A:0:A'))
|
||||
'a::a:0:a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:0:A:0:0:A:0'))
|
||||
'a::a:0:0:a:0'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'A:0:A:0:A:0:A:0'))
|
||||
'a:0:a:0:a:0:a:0'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '0:A:0:A:0:A:0:A'))
|
||||
'0:a:0:a:0:a:0:a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, '1080:0:0:0:8:800:200C:417A'))
|
||||
'1080::8:800:200c:417a'
|
||||
|
||||
>>> inet_ntop(AF_INET6, inet_pton(AF_INET6, 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210'))
|
||||
'fedc:ba98:7654:3210:fedc:ba98:7654:3210'
|
||||
|
||||
}}}
|
||||
|
||||
IPv4 failure tests
|
||||
|
||||
{{{
|
||||
|
||||
>>> inet_ntoa(1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: string type expected, not <class 'int'>
|
||||
|
||||
|
||||
>>> inet_ntoa('\x00')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: invalid length of packed IP address string
|
||||
|
||||
>>> inet_aton('0x0')
|
||||
b'\x00\x00\x00\x00'
|
||||
|
||||
>>> inet_aton('010')
|
||||
b'\x08\x00\x00\x00'
|
||||
|
||||
}}}
|
||||
|
||||
IPv6 failure tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> inet_pton(AF_INET6, '::0x07f')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: illegal IP address string '::0x07f'
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,108 @@
|
||||
=IP Subnet Tests=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
|
||||
}}}
|
||||
|
||||
Incrementing IP objects.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.0/28')
|
||||
>>> for i in range(16):
|
||||
... str(ip)
|
||||
... ip += 1
|
||||
'192.0.2.0/28'
|
||||
'192.0.2.16/28'
|
||||
'192.0.2.32/28'
|
||||
'192.0.2.48/28'
|
||||
'192.0.2.64/28'
|
||||
'192.0.2.80/28'
|
||||
'192.0.2.96/28'
|
||||
'192.0.2.112/28'
|
||||
'192.0.2.128/28'
|
||||
'192.0.2.144/28'
|
||||
'192.0.2.160/28'
|
||||
'192.0.2.176/28'
|
||||
'192.0.2.192/28'
|
||||
'192.0.2.208/28'
|
||||
'192.0.2.224/28'
|
||||
'192.0.2.240/28'
|
||||
|
||||
>>> ip = IPNetwork('2001:470:1f04::/48')
|
||||
>>> for i in ip.subnet(128):
|
||||
... print (i)
|
||||
... break
|
||||
2001:470:1f04::/128
|
||||
|
||||
}}}
|
||||
|
||||
IP address and subnet sortability.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip_list = []
|
||||
|
||||
>>> for subnet in IPNetwork('192.0.2.0/24').subnet(28, 3):
|
||||
... ip_list.append(subnet)
|
||||
... ip_list.extend([ip for ip in subnet])
|
||||
|
||||
>>> for addr in sorted(ip_list):
|
||||
... print('%r' % addr)
|
||||
IPNetwork('192.0.2.0/28')
|
||||
IPAddress('192.0.2.0')
|
||||
IPAddress('192.0.2.1')
|
||||
IPAddress('192.0.2.2')
|
||||
IPAddress('192.0.2.3')
|
||||
IPAddress('192.0.2.4')
|
||||
IPAddress('192.0.2.5')
|
||||
IPAddress('192.0.2.6')
|
||||
IPAddress('192.0.2.7')
|
||||
IPAddress('192.0.2.8')
|
||||
IPAddress('192.0.2.9')
|
||||
IPAddress('192.0.2.10')
|
||||
IPAddress('192.0.2.11')
|
||||
IPAddress('192.0.2.12')
|
||||
IPAddress('192.0.2.13')
|
||||
IPAddress('192.0.2.14')
|
||||
IPAddress('192.0.2.15')
|
||||
IPNetwork('192.0.2.16/28')
|
||||
IPAddress('192.0.2.16')
|
||||
IPAddress('192.0.2.17')
|
||||
IPAddress('192.0.2.18')
|
||||
IPAddress('192.0.2.19')
|
||||
IPAddress('192.0.2.20')
|
||||
IPAddress('192.0.2.21')
|
||||
IPAddress('192.0.2.22')
|
||||
IPAddress('192.0.2.23')
|
||||
IPAddress('192.0.2.24')
|
||||
IPAddress('192.0.2.25')
|
||||
IPAddress('192.0.2.26')
|
||||
IPAddress('192.0.2.27')
|
||||
IPAddress('192.0.2.28')
|
||||
IPAddress('192.0.2.29')
|
||||
IPAddress('192.0.2.30')
|
||||
IPAddress('192.0.2.31')
|
||||
IPNetwork('192.0.2.32/28')
|
||||
IPAddress('192.0.2.32')
|
||||
IPAddress('192.0.2.33')
|
||||
IPAddress('192.0.2.34')
|
||||
IPAddress('192.0.2.35')
|
||||
IPAddress('192.0.2.36')
|
||||
IPAddress('192.0.2.37')
|
||||
IPAddress('192.0.2.38')
|
||||
IPAddress('192.0.2.39')
|
||||
IPAddress('192.0.2.40')
|
||||
IPAddress('192.0.2.41')
|
||||
IPAddress('192.0.2.42')
|
||||
IPAddress('192.0.2.43')
|
||||
IPAddress('192.0.2.44')
|
||||
IPAddress('192.0.2.45')
|
||||
IPAddress('192.0.2.46')
|
||||
IPAddress('192.0.2.47')
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,993 @@
|
||||
=IP Address Tutorial=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
This unit test serves as both testing of the netaddr API and an executable tutorial courtesy of the doctest and unittest modules in the Python standard library.
|
||||
|
||||
Let's start with the standard module import.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr import *
|
||||
>>> import pprint
|
||||
|
||||
}}}
|
||||
|
||||
You can safely import everything from the netaddr namespace as care has been taken to only export the necessary classes, functions and constants.
|
||||
|
||||
You can always hand pick them if you are unsure about possible name clashes.
|
||||
|
||||
We install the standard library module `pprint` to help format some output.
|
||||
|
||||
|
||||
==Basic IP Address Operations==
|
||||
|
||||
This IP object represents a single address.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress('192.0.2.1')
|
||||
|
||||
>>> ip.version
|
||||
4
|
||||
|
||||
}}}
|
||||
|
||||
Standard `repr()` access returns a Python statement that can reconstruct an equivalent IP address object from scratch if executed in the Python interpreter.
|
||||
|
||||
{{{
|
||||
|
||||
>>> repr(ip)
|
||||
"IPAddress('192.0.2.1')"
|
||||
|
||||
>>> ip
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
}}}
|
||||
|
||||
Access in the string context returns the IP object as a string value.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str(ip)
|
||||
'192.0.2.1'
|
||||
|
||||
>>> '%s' % ip
|
||||
'192.0.2.1'
|
||||
|
||||
>>> ip.format() # only really useful for IPv6 addresses.
|
||||
'192.0.2.1'
|
||||
|
||||
}}}
|
||||
|
||||
==IP Address Numerical Representations==
|
||||
|
||||
You can view an IP address in various other formats.
|
||||
|
||||
{{{
|
||||
|
||||
>>> int(ip) == 3221225985
|
||||
True
|
||||
|
||||
>>> hex(ip)
|
||||
'0xc0000201'
|
||||
|
||||
>>> ip.bin
|
||||
'0b11000000000000000000001000000001'
|
||||
|
||||
>>> ip.bits()
|
||||
'11000000.00000000.00000010.00000001'
|
||||
|
||||
>>> ip.words == (192, 0, 2, 1)
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
==Representing IP Subnets==
|
||||
|
||||
IPNetwork objects are used to represent subnets that accept netmasks and CIDR prefixes.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.1')
|
||||
|
||||
>>> ip.ip
|
||||
IPAddress('192.0.2.1')
|
||||
|
||||
>>> ip.network, ip.broadcast
|
||||
(IPAddress('192.0.2.1'), IPAddress('192.0.2.1'))
|
||||
|
||||
>>> ip.netmask, ip.hostmask
|
||||
(IPAddress('255.255.255.255'), IPAddress('0.0.0.0'))
|
||||
|
||||
>>> ip.size
|
||||
1
|
||||
|
||||
}}}
|
||||
|
||||
In this case, the network and broadcast address are the same, akin to a host route.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.0/24')
|
||||
|
||||
>>> ip.ip
|
||||
IPAddress('192.0.2.0')
|
||||
|
||||
>>> ip.network, ip.broadcast
|
||||
(IPAddress('192.0.2.0'), IPAddress('192.0.2.255'))
|
||||
|
||||
>>> ip.netmask, ip.hostmask
|
||||
(IPAddress('255.255.255.0'), IPAddress('0.0.0.255'))
|
||||
|
||||
>>> ip.size
|
||||
256
|
||||
|
||||
}}}
|
||||
|
||||
And finally, this IPNetwork object represents an IP address that belongs to a given IP subnet.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.3.112/22')
|
||||
|
||||
>>> ip.ip
|
||||
IPAddress('192.0.3.112')
|
||||
|
||||
>>> ip.network, ip.broadcast
|
||||
(IPAddress('192.0.0.0'), IPAddress('192.0.3.255'))
|
||||
|
||||
>>> ip.netmask, ip.hostmask
|
||||
(IPAddress('255.255.252.0'), IPAddress('0.0.3.255'))
|
||||
|
||||
>>> ip.size
|
||||
1024
|
||||
|
||||
}}}
|
||||
|
||||
Internally, each IPNetwork object only stores 3 values :-
|
||||
|
||||
* the IP address value as an unsigned integer
|
||||
* a reference to the IP protocol module for the IP version being represented
|
||||
* the network CIDR prefix bitmask
|
||||
|
||||
All the other values are calculated on-the-fly as they are accessed.
|
||||
|
||||
It is possible to adjust the IP address value and the CIDR prefix after object instantiation.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('0.0.0.0/0')
|
||||
|
||||
>>> ip
|
||||
IPNetwork('0.0.0.0/0')
|
||||
|
||||
>>> ip.value = 3221225985
|
||||
|
||||
>>> ip
|
||||
IPNetwork('192.0.2.1/0')
|
||||
|
||||
>>> ip.prefixlen
|
||||
0
|
||||
|
||||
>>> ip.prefixlen = 23
|
||||
|
||||
>>> ip
|
||||
IPNetwork('192.0.2.1/23')
|
||||
|
||||
}}}
|
||||
|
||||
There is also a property that lets you access the *true* CIDR address which removes all host bits from the network address based on the CIDR subnet prefix.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip.cidr
|
||||
IPNetwork('192.0.2.0/23')
|
||||
|
||||
}}}
|
||||
|
||||
This is handy for specifying some networking configurations correctly.
|
||||
|
||||
If you want to access information about each of the various IP addresses that form the IP subnet, this is available by performing pass through calls to sub methods of each `IPAddress` object.
|
||||
|
||||
For example if you want to see a binary digit representation of each address you can do the following.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip.ip.bits()
|
||||
'11000000.00000000.00000010.00000001'
|
||||
|
||||
>>> ip.network.bits()
|
||||
'11000000.00000000.00000010.00000000'
|
||||
|
||||
>>> ip.netmask.bits()
|
||||
'11111111.11111111.11111110.00000000'
|
||||
|
||||
>>> ip.broadcast.bits()
|
||||
'11000000.00000000.00000011.11111111'
|
||||
|
||||
}}}
|
||||
|
||||
==IPv6 support==
|
||||
|
||||
Full support for IPv6 addressing is provided as well. To prove this, let's try a few examples.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress(0, 6)
|
||||
|
||||
>>> ip
|
||||
IPAddress('::')
|
||||
|
||||
>>> ip = IPNetwork('fe80::dead:beef/64')
|
||||
|
||||
>>> str(ip), ip.prefixlen, ip.version
|
||||
('fe80::dead:beef/64', 64, 6)
|
||||
|
||||
>>> int(ip.ip) == 338288524927261089654018896845083623151
|
||||
True
|
||||
|
||||
>>> hex(ip.ip)
|
||||
'0xfe8000000000000000000000deadbeef'
|
||||
|
||||
}}}
|
||||
|
||||
Bit-style output isn't as quite as friendly as hexadecimal for such a long numbers, but here the proof that it works!
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip.ip.bits()
|
||||
'1111111010000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1101111010101101:1011111011101111'
|
||||
|
||||
}}}
|
||||
|
||||
Here are some networking details for an IPv6 subnet.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip.network, ip.broadcast, ip.netmask, ip.hostmask
|
||||
(IPAddress('fe80::'), IPAddress('fe80::ffff:ffff:ffff:ffff'), IPAddress('ffff:ffff:ffff:ffff::'), IPAddress('::ffff:ffff:ffff:ffff'))
|
||||
|
||||
}}}
|
||||
|
||||
==IPv4 / IPv6 Interoperability==
|
||||
|
||||
It is likely that with IPv6 becoming more prevalent, you'll want to be able to interoperate between IPv4 and IPv6 address seemlessly.
|
||||
|
||||
Here are a couple of methods that help achieve this.
|
||||
|
||||
===IPv4 to IPv6===
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.15').ipv4()
|
||||
IPAddress('192.0.2.15')
|
||||
|
||||
>>> IPAddress('192.0.2.15').ipv6()
|
||||
IPAddress('::ffff:192.0.2.15')
|
||||
|
||||
>>> IPAddress('192.0.2.15').ipv6(ipv4_compatible=True)
|
||||
IPAddress('::192.0.2.15')
|
||||
|
||||
>>> IPAddress('192.0.2.15').ipv6(True)
|
||||
IPAddress('::192.0.2.15')
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.1/23')
|
||||
|
||||
>>> ip.ipv4()
|
||||
IPNetwork('192.0.2.1/23')
|
||||
|
||||
>>> ip.ipv6()
|
||||
IPNetwork('::ffff:192.0.2.1/119')
|
||||
|
||||
>>> ip.ipv6(ipv4_compatible=True)
|
||||
IPNetwork('::192.0.2.1/119')
|
||||
|
||||
}}}
|
||||
|
||||
===IPv6 to IPv4===
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('::ffff:192.0.2.1/119').ipv4()
|
||||
IPNetwork('192.0.2.1/23')
|
||||
|
||||
>>> IPNetwork('::192.0.2.1/119').ipv4()
|
||||
IPNetwork('192.0.2.1/23')
|
||||
|
||||
}}}
|
||||
|
||||
Note that the IP object returns IPv4 "mapped" addresses by default in preference to IPv4 "compatible" ones. This has been chosen purposefully as the latter form has been deprecated (see RFC 4291 for details).
|
||||
|
||||
==List Operations On IP Objects==
|
||||
|
||||
If you treat an IP network object as if it were a standard Python list object it will give you access to a list of individual IP address objects. This of course is illusory and they are not created until you access them.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.16/29')
|
||||
|
||||
}}}
|
||||
|
||||
Accessing an IP object using the list() context invokes the default generator which returns a list of all IP objects in the range specified by the IP object's subnet.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip_list = list(ip)
|
||||
|
||||
>>> len(ip_list)
|
||||
8
|
||||
|
||||
>>> ip_list
|
||||
[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), ..., IPAddress('192.0.2.22'), IPAddress('192.0.2.23')]
|
||||
|
||||
}}}
|
||||
|
||||
The length of that list is 8 individual IP addresses.
|
||||
|
||||
{{{
|
||||
|
||||
>>> len(ip)
|
||||
8
|
||||
|
||||
}}}
|
||||
|
||||
You can use standard index access to IP addresses in the subnet.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip[0]
|
||||
IPAddress('192.0.2.16')
|
||||
|
||||
>>> ip[1]
|
||||
IPAddress('192.0.2.17')
|
||||
|
||||
>>> ip[-1]
|
||||
IPAddress('192.0.2.23')
|
||||
|
||||
}}}
|
||||
|
||||
You can even uses extended slices on IP addresses in the subnet.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip[0:4]
|
||||
<generator object ...>
|
||||
|
||||
}}}
|
||||
|
||||
The slice is actually a generator function. This is to save time and system resources. Some slices can obviously end up being extremely large for some subnets!
|
||||
|
||||
Here is how you'd access all elements in a slice.
|
||||
|
||||
{{{
|
||||
|
||||
>>> list(ip[0:4])
|
||||
[IPAddress('192.0.2.16'), IPAddress('192.0.2.17'), IPAddress('192.0.2.18'), IPAddress('192.0.2.19')]
|
||||
|
||||
}}}
|
||||
|
||||
Extended slicing is also supported.
|
||||
|
||||
{{{
|
||||
|
||||
>>> list(ip[0::2])
|
||||
[IPAddress('192.0.2.16'), IPAddress('192.0.2.18'), IPAddress('192.0.2.20'), IPAddress('192.0.2.22')]
|
||||
|
||||
}}}
|
||||
|
||||
List reversal.
|
||||
|
||||
{{{
|
||||
|
||||
>>> list(ip[-1::-1])
|
||||
[IPAddress('192.0.2.23'), IPAddress('192.0.2.22'), ..., IPAddress('192.0.2.17'), IPAddress('192.0.2.16')]
|
||||
|
||||
}}}
|
||||
|
||||
Use of generators ensures working with large IP subnets is efficient.
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in IPNetwork('192.0.2.0/23'):
|
||||
... print('%s' % ip)
|
||||
...
|
||||
192.0.2.0
|
||||
192.0.2.1
|
||||
192.0.2.2
|
||||
192.0.2.3
|
||||
...
|
||||
192.0.3.252
|
||||
192.0.3.253
|
||||
192.0.3.254
|
||||
192.0.3.255
|
||||
|
||||
}}}
|
||||
|
||||
In IPv4 networks you only usually assign the addresses between the network and broadcast addresses to actual host interfaces on systems.
|
||||
|
||||
Here is the iterator provided for accessing these IP addresses :-
|
||||
|
||||
{{{
|
||||
|
||||
>>> for ip in IPNetwork('192.0.2.0/23').iter_hosts():
|
||||
... print('%s' % ip)
|
||||
...
|
||||
192.0.2.1
|
||||
192.0.2.2
|
||||
192.0.2.3
|
||||
192.0.2.4
|
||||
...
|
||||
192.0.3.251
|
||||
192.0.3.252
|
||||
192.0.3.253
|
||||
192.0.3.254
|
||||
|
||||
}}}
|
||||
|
||||
==Sorting Collection Of IP Objects==
|
||||
|
||||
It is fairly common and useful to be able to sort IP addresses correctly (in numerical order).
|
||||
|
||||
Here is how sorting works with individual addresses.
|
||||
|
||||
{{{
|
||||
|
||||
>>> import random
|
||||
|
||||
>>> ip_list = list(IPNetwork('192.0.2.128/28'))
|
||||
|
||||
>>> random.shuffle(ip_list)
|
||||
|
||||
>>> sorted(ip_list)
|
||||
[IPAddress('192.0.2.128'), IPAddress('192.0.2.129'), ..., IPAddress('192.0.2.142'), IPAddress('192.0.2.143')]
|
||||
|
||||
}}}
|
||||
|
||||
You can just as easily sort IP subnets at the same time, including combinations of IPv4 and IPv6 addresses as well.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip_list = [
|
||||
... IPAddress('192.0.2.130'),
|
||||
... IPAddress('10.0.0.1'),
|
||||
... IPNetwork('192.0.2.128/28'),
|
||||
... IPNetwork('192.0.3.0/24'),
|
||||
... IPNetwork('192.0.2.0/24'),
|
||||
... IPNetwork('fe80::/64'),
|
||||
... IPAddress('::'),
|
||||
... IPNetwork('172.24/12')]
|
||||
|
||||
>>> random.shuffle(ip_list)
|
||||
|
||||
>>> ip_list.sort()
|
||||
|
||||
>>> pprint.pprint(ip_list)
|
||||
[IPAddress('10.0.0.1'),
|
||||
IPNetwork('172.24.0.0/12'),
|
||||
IPNetwork('192.0.2.0/24'),
|
||||
IPNetwork('192.0.2.128/28'),
|
||||
IPAddress('192.0.2.130'),
|
||||
IPNetwork('192.0.3.0/24'),
|
||||
IPAddress('::'),
|
||||
IPNetwork('fe80::/64')]
|
||||
|
||||
}}}
|
||||
|
||||
Notice how IPv4 is ordered before IPv6 and overlapping subnets sort in order from largest subnet to smallest.
|
||||
|
||||
==Merging IP Addresses And Subnets==
|
||||
|
||||
Another useful operation is the ability to summarize groups of IP subnets and addresses, merging them together where possible to create the smallest possible list of CIDR subnets.
|
||||
|
||||
Here is how to do this using the `cidr_merge()` function.
|
||||
|
||||
First we create a list of IP objects that is a good mix of individual addresses and subnets, along with some string based IP address values for good measure. To make things more challenging some IPv6 addresses have been included as well.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip_list = [ip for ip in IPNetwork('fe80::/120')]
|
||||
|
||||
>>> ip_list.append(IPNetwork('192.0.2.0/24'))
|
||||
|
||||
>>> ip_list.extend([str(ip) for ip in IPNetwork('192.0.3.0/24')])
|
||||
|
||||
>>> ip_list.append(IPNetwork('192.0.4.0/25'))
|
||||
|
||||
>>> ip_list.append(IPNetwork('192.0.4.128/25'))
|
||||
|
||||
>>> len(ip_list)
|
||||
515
|
||||
|
||||
>>> cidr_merge(ip_list)
|
||||
[IPNetwork('192.0.2.0/23'), IPNetwork('192.0.4.0/24'), IPNetwork('fe80::/120')]
|
||||
|
||||
}}}
|
||||
|
||||
==Dealing With Arbitrary Lists Of IP Objects==
|
||||
|
||||
While CIDR subnets are a useful construct, sometimes it is necessarily (particularly with IPv4 which predates the CIDR specification) to be able to generate lists of IP addresses that have an arbitrary start and end address that do not fall on bit mask boundaries.
|
||||
|
||||
The iter_iprange() function allow you to do just this.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip_list = list(iter_iprange('192.0.2.1', '192.0.2.14'))
|
||||
|
||||
>>> len(ip_list)
|
||||
14
|
||||
|
||||
>>> ip_list
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), ..., IPAddress('192.0.2.13'), IPAddress('192.0.2.14')]
|
||||
|
||||
}}}
|
||||
|
||||
It is equally nice to know what the actual list of CIDR subnets is that would correctly cover this non-aligned range of addresses.
|
||||
|
||||
Here `cidr_merge()` comes to the rescue.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidr_merge(ip_list)
|
||||
[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/30'), IPNetwork('192.0.2.12/31'), IPNetwork('192.0.2.14/32')]
|
||||
|
||||
}}}
|
||||
|
||||
==IP Subnetting And Supernetting==
|
||||
|
||||
It is quite common to have a large CIDR subnet that you may want to split up into multiple smaller component blocks to better manage your networks.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('172.24.0.0/16')
|
||||
|
||||
>>> ip.subnet(23)
|
||||
<generator object ...>
|
||||
|
||||
}}}
|
||||
|
||||
Again this method produces and iterator because of the possibility for a large number of return values.
|
||||
|
||||
{{{
|
||||
|
||||
>>> subnets = list(ip.subnet(23))
|
||||
|
||||
>>> len(subnets)
|
||||
128
|
||||
|
||||
>>> subnets
|
||||
[IPNetwork('172.24.0.0/23'), IPNetwork('172.24.2.0/23'), IPNetwork('172.24.4.0/23'), ..., IPNetwork('172.24.250.0/23'), IPNetwork('172.24.252.0/23'), IPNetwork('172.24.254.0/23')]
|
||||
|
||||
}}}
|
||||
|
||||
It is also possible to retrieve the list of supernets that a given IP address or subnet belongs to (with an optional limit).
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPNetwork('192.0.2.114')
|
||||
|
||||
>>> supernets = ip.supernet(22)
|
||||
|
||||
>>> pprint.pprint(supernets)
|
||||
[IPNetwork('192.0.0.0/22'),
|
||||
IPNetwork('192.0.2.0/23'),
|
||||
IPNetwork('192.0.2.0/24'),
|
||||
IPNetwork('192.0.2.0/25'),
|
||||
IPNetwork('192.0.2.64/26'),
|
||||
IPNetwork('192.0.2.96/27'),
|
||||
IPNetwork('192.0.2.112/28'),
|
||||
IPNetwork('192.0.2.112/29'),
|
||||
IPNetwork('192.0.2.112/30'),
|
||||
IPNetwork('192.0.2.114/31')]
|
||||
|
||||
}}}
|
||||
|
||||
This method returns a list because the potential list of values is of a predictable size (no more than 31 CIDRs for an IPv4 address and 127 for IPv6).
|
||||
|
||||
==Dealing With Less Common IP Network Specifications==
|
||||
|
||||
Until the advent of the CIDR specification it was common to infer the netmask of an IPv4 address based on its first octet using an set of classful rules.
|
||||
|
||||
It is common to come across these in various RFCs and they are well supported by a number of software libraries. Rather than leave out this important (mainly historical) set of rules they are catered for using the cidr_abbrev_to_verbose() function.
|
||||
|
||||
Here is an example of these rules for the whole of the IPv4 address space.
|
||||
|
||||
{{{
|
||||
|
||||
>>> cidrs = [cidr_abbrev_to_verbose(octet) for octet in range(0, 256)]
|
||||
|
||||
>>> pprint.pprint(cidrs)
|
||||
['0.0.0.0/8',
|
||||
...
|
||||
'127.0.0.0/8',
|
||||
'128.0.0.0/16',
|
||||
...
|
||||
'191.0.0.0/16',
|
||||
'192.0.0.0/24',
|
||||
...
|
||||
'223.0.0.0/24',
|
||||
'224.0.0.0/4',
|
||||
...
|
||||
'239.0.0.0/4',
|
||||
'240.0.0.0/32',
|
||||
...
|
||||
'255.0.0.0/32']
|
||||
|
||||
>>> len(cidrs)
|
||||
256
|
||||
|
||||
}}}
|
||||
|
||||
==IP Address Categories==
|
||||
|
||||
IP addresses fall several broad categories and not all are suitable for assignment as system interface addresses.
|
||||
|
||||
Unicast
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.1').is_unicast()
|
||||
True
|
||||
|
||||
>>> IPAddress('fe80::1').is_unicast()
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Multicast
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('239.192.0.1').is_multicast()
|
||||
True
|
||||
|
||||
>>> IPAddress('ff00::1').is_multicast()
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Private
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('172.24.0.1').is_private()
|
||||
True
|
||||
|
||||
>>> IPAddress('10.0.0.1').is_private()
|
||||
True
|
||||
|
||||
>>> IPAddress('192.168.0.1').is_private()
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Reserved
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('253.0.0.1').is_reserved()
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Public (Internet) addresses.
|
||||
|
||||
Note that not all of these may be allocated by the various regional Internet registrars.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ip = IPAddress('62.125.24.5')
|
||||
|
||||
>>> ip.is_unicast() and not ip.is_private()
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
There are also other types of addresses that have specific functions e.g. masking
|
||||
|
||||
Netmasks
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('255.255.254.0').is_netmask()
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Hostmasks
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('0.0.1.255').is_hostmask()
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Loopback addresses
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('127.0.0.1').is_loopback()
|
||||
True
|
||||
|
||||
>>> IPAddress('::1').is_loopback()
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
==IP address comparisons==
|
||||
|
||||
IP objects can be compared with each other. As an IP object can represent both an individual IP address and an implicit network, it pays to get both sides of your comparison into the same terms before you compare them to avoid any odd results.
|
||||
|
||||
Here are some comparisons of individual IP address to get the ball rolling.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.1') == IPAddress('192.0.2.1')
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.1') < IPAddress('192.0.2.2')
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.2') > IPAddress('192.0.2.1')
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.1') != IPAddress('192.0.2.1')
|
||||
False
|
||||
|
||||
>>> IPAddress('192.0.2.1') >= IPAddress('192.0.2.1')
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.2') >= IPAddress('192.0.2.1')
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.1') <= IPAddress('192.0.2.1')
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.1') <= IPAddress('192.0.2.2')
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Now lets try something a little more interesting.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') == IPNetwork('192.0.2.112/24')
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Hmmmmmmmm... looks a bit odd doesn't it? That's because by default, IP objects compare their subnets (or lower and upper boundaries) rather than their individual IP address values.
|
||||
|
||||
The solution to this situation is very simple. Knowing this default behaviour, just be explicit about exactly which portion of each IP object you'd like to compare using pass-through properties.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24').ip == IPNetwork('192.0.2.112/24').ip
|
||||
False
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24').ip < IPNetwork('192.0.2.112/24').ip
|
||||
True
|
||||
|
||||
That's more like it. You can also be explicit about comparing networks in this way if you so wish (although it is not strictly necessary).
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24').cidr == IPNetwork('192.0.2.112/24').cidr
|
||||
True
|
||||
|
||||
Armed with this information here are some examples of network comparisons.
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') == IPNetwork('192.0.3.0/24')
|
||||
False
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') < IPNetwork('192.0.3.0/24')
|
||||
True
|
||||
|
||||
>>> IPNetwork('192.0.2.0/24') < IPNetwork('192.0.3.0/24')
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
This will inevitably raise questions about comparing IPAddress (scalar) objects and IPNetwork (vector) objects with each other (or at least it should).
|
||||
|
||||
Here is how netaddr chooses to address this situation.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32')
|
||||
False
|
||||
|
||||
>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32')
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
An IP network or subnet is different from an individual IP address and therefore cannot be (directly) compared.
|
||||
|
||||
If you want to compare them successfully, you must be explicit about which aspect of the IP network you wish to match against the IP address in question.
|
||||
|
||||
You can use the index of the first or last address if it is a /32 like so :-
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32')[0]
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32')[-1]
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32')[0]
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
You can also use the base address if this is what you wish to compare :-
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('192.0.2.0') == IPNetwork('192.0.2.0/32').ip
|
||||
True
|
||||
|
||||
>>> IPAddress('192.0.2.0') != IPNetwork('192.0.2.0/32').ip
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
While this may seem a bit pointless at first, netaddr strives to keep IP addresses and network separate from one another while still allowing reasonable interoperability.
|
||||
|
||||
==Interaction with DNS==
|
||||
|
||||
It is a common administrative task to Generating reverse IP lookups for DNS. This is particularly arduous for IPv6 addresses.
|
||||
|
||||
Here is how you do this using the IP object's `reverse_dns()` method.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPAddress('172.24.0.13').reverse_dns
|
||||
'13.0.24.172.in-addr.arpa.'
|
||||
|
||||
>>> IPAddress('fe80::feeb:daed').reverse_dns
|
||||
'd.e.a.d.b.e.e.f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6.arpa.'
|
||||
|
||||
}}}
|
||||
|
||||
Note that ip6.int is not used as this has been deprecated (see RFC 3152 for details).
|
||||
|
||||
==Non standard IP address range types==
|
||||
|
||||
As CIDR is a relative newcomer given the long history of IP version 4 you are quite likely to come across systems and documentation which make reference to IP address ranges in formats other than CIDR. Converting from these arbitrary range types to CIDR and back again isn't a particularly fun task. Fortunately, netaddr tries to make this job easy for both you and your end users with two purpose built classes.
|
||||
|
||||
===Arbitrary IP Address Ranges===
|
||||
|
||||
You can represent an arbitrary IP address range using a lower and upper bound address in the form of an IPRange object.
|
||||
|
||||
{{{
|
||||
|
||||
>>> r1 = IPRange('192.0.2.1', '192.0.2.15')
|
||||
|
||||
>>> r1
|
||||
IPRange('192.0.2.1', '192.0.2.15')
|
||||
|
||||
}}}
|
||||
|
||||
You can iterate across and index these ranges just like and IPNetwork object.
|
||||
|
||||
Importantly, you can also convert it to it's CIDR equivalent.
|
||||
|
||||
{{{
|
||||
|
||||
>>> r1.cidrs()
|
||||
[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')]
|
||||
|
||||
}}}
|
||||
|
||||
Here is how individual IPRange and IPNetwork compare.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPRange('192.0.2.0', '192.0.2.255') != IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
>>> IPRange('192.0.2.0', '192.0.2.255') == IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
You may wish to compare an IP range against a list of IPAddress and IPNetwork
|
||||
objects.
|
||||
|
||||
{{{
|
||||
|
||||
>>> r1 = IPRange('192.0.2.1', '192.0.2.15')
|
||||
|
||||
>>> addrs = list(r1)
|
||||
|
||||
>>> addrs
|
||||
[IPAddress('192.0.2.1'), IPAddress('192.0.2.2'), IPAddress('192.0.2.3'), IPAddress('192.0.2.4'), IPAddress('192.0.2.5'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), IPAddress('192.0.2.8'), IPAddress('192.0.2.9'), IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPAddress('192.0.2.12'), IPAddress('192.0.2.13'), IPAddress('192.0.2.14'), IPAddress('192.0.2.15')]
|
||||
|
||||
>>> r1 == addrs
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
Oops! Not quite what we were looking for or expecting.
|
||||
|
||||
The way to do this is to get either side of the comparison operation into the same terms.
|
||||
|
||||
{{{
|
||||
|
||||
>>> list(r1) == addrs
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
That's more like it.
|
||||
|
||||
The same goes for IPNetwork objects.
|
||||
|
||||
{{{
|
||||
|
||||
>>> subnets = r1.cidrs()
|
||||
|
||||
>>> subnets
|
||||
[IPNetwork('192.0.2.1/32'), IPNetwork('192.0.2.2/31'), IPNetwork('192.0.2.4/30'), IPNetwork('192.0.2.8/29')]
|
||||
|
||||
>>> r1 == subnets
|
||||
False
|
||||
|
||||
>>> r1.cidrs() == subnets
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
The above works if the list you are comparing contains one type or the other, but what if you have a mixed list of IPAddress, IPNetwork and string addresses?
|
||||
|
||||
Time for some slightly more powerful operations. Let's make use of a new class for dealing with groups of IP addresses and subnets. The IPSet class.
|
||||
|
||||
{{{
|
||||
|
||||
>>> ips = [IPAddress('192.0.2.1'), '192.0.2.2/31', IPNetwork('192.0.2.4/31'), IPAddress('192.0.2.6'), IPAddress('192.0.2.7'), '192.0.2.8', '192.0.2.9', IPAddress('192.0.2.10'), IPAddress('192.0.2.11'), IPNetwork('192.0.2.12/30')]
|
||||
|
||||
>>> s1 = IPSet(r1.cidrs())
|
||||
|
||||
>>> s2 = IPSet(ips)
|
||||
|
||||
>>> s2
|
||||
IPSet(['192.0.2.1/32', '192.0.2.2/31', '192.0.2.4/30', '192.0.2.8/29'])
|
||||
|
||||
>>> s1 == s2
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Let's remove one of the element from one of the IPSet objects and see what happens.
|
||||
|
||||
{{{
|
||||
|
||||
>>> s2.pop()
|
||||
IPNetwork('192.0.2.4/30')
|
||||
|
||||
>>> s1 == s2
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
This is perhaps a somewhat contrived example but it just shows you some of the capabilities on offer.
|
||||
|
||||
See the IPSet tutorial in the wiki for more details on that class.
|
||||
|
||||
===IP Globs===
|
||||
|
||||
netaddr also supports a user friendly form of specifying IP address ranges using a glob style syntax. Please note that at the current time this only supports IPv4.
|
||||
|
||||
{{{
|
||||
|
||||
>>> IPGlob('192.0.2.*') == IPNetwork('192.0.2.0/24')
|
||||
True
|
||||
|
||||
IPGlob('192.0.2.*') != IPNetwork('192.0.2.0/24')
|
||||
False
|
||||
|
||||
}}}
|
||||
|
||||
As IPGlob is a subclass of IPRange, all of the same operations apply.
|
||||
@@ -0,0 +1,93 @@
|
||||
=IEEE EUI-48 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy.eui48 import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Smoke Tests==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '00000000-00001111-00011111-00010010-11100111-00110011'
|
||||
>>> i = 64945841971
|
||||
>>> t = (0x0, 0x0f, 0x1f, 0x12, 0xe7, 0x33)
|
||||
>>> s = '00-0F-1F-12-E7-33'
|
||||
>>> p = b'\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> bits_to_int(b) == 64945841971
|
||||
True
|
||||
|
||||
>>> int_to_bits(i) == b
|
||||
True
|
||||
|
||||
>>> int_to_str(i)
|
||||
'00-0F-1F-12-E7-33'
|
||||
|
||||
>>> int_to_words(i)
|
||||
(0, 15, 31, 18, 231, 51)
|
||||
|
||||
>>> int_to_packed(i)
|
||||
b'\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> str_to_int(s) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(t) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t)) == 64945841971
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 64945841971
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
==Smoke Tests With Alternate Dialects==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '00000000:00001111:00011111:00010010:11100111:00110011'
|
||||
>>> i = 64945841971
|
||||
>>> t = (0x0, 0x0f, 0x1f, 0x12, 0xe7, 0x33)
|
||||
>>> s = '0:f:1f:12:e7:33'
|
||||
>>> p = b'\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> bits_to_int(b, mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> int_to_bits(i, mac_unix) == b
|
||||
True
|
||||
|
||||
>>> int_to_str(i, mac_unix)
|
||||
'0:f:1f:12:e7:33'
|
||||
|
||||
>>> int_to_str(i, mac_cisco)
|
||||
'000f.1f12.e733'
|
||||
|
||||
>>> int_to_str(i, mac_unix)
|
||||
'0:f:1f:12:e7:33'
|
||||
|
||||
>>> int_to_words(i, mac_unix)
|
||||
(0, 15, 31, 18, 231, 51)
|
||||
|
||||
>>> int_to_packed(i)
|
||||
b'\x00\x0f\x1f\x12\xe73'
|
||||
|
||||
>>> str_to_int(s) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(t, mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t), mac_unix) == 64945841971
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 64945841971
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,130 @@
|
||||
=IP version 4 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
Uses TEST-NET references throughout, as described in RFC 3330.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy.ipv4 import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Smoke Tests==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '11000000.00000000.00000010.00000001'
|
||||
>>> i = 3221225985
|
||||
>>> t = (192, 0, 2, 1)
|
||||
>>> s = '192.0.2.1'
|
||||
>>> p = b'\xc0\x00\x02\x01'
|
||||
>>> bin_val = '0b11000000000000000000001000000001'
|
||||
|
||||
>>> bits_to_int(b) == 3221225985
|
||||
True
|
||||
|
||||
>>> int_to_bits(i)
|
||||
'11000000.00000000.00000010.00000001'
|
||||
|
||||
>>> int_to_str(i)
|
||||
'192.0.2.1'
|
||||
|
||||
>>> int_to_words(i) == (192, 0, 2, 1)
|
||||
True
|
||||
|
||||
>>> int_to_packed(i)
|
||||
b'\xc0\x00\x02\x01'
|
||||
|
||||
>>> int_to_bin(i)
|
||||
'0b11000000000000000000001000000001'
|
||||
|
||||
>>> int_to_bin(i)
|
||||
'0b11000000000000000000001000000001'
|
||||
|
||||
>>> bin_to_int(bin_val) == 3221225985
|
||||
True
|
||||
|
||||
>>> words_to_int(t) == 3221225985
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t)) == 3221225985
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 3221225985
|
||||
True
|
||||
|
||||
>>> valid_bin(bin_val)
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
== inet_aton() Behavioural Tests ==
|
||||
|
||||
inet_aton() is a very old system call and is very permissive with regard to what is assume is a valid IPv4 address. Unfortunately, it is also the most widely used by system software used in software today, so netaddr supports this behaviour by default.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str_to_int('127') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('0x7f') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('0177') == 127
|
||||
True
|
||||
|
||||
>>> str_to_int('127.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('0x7f.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('0177.1') == 2130706433
|
||||
True
|
||||
|
||||
>>> str_to_int('127.0.0.1') == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
== inet_pton() Behavioural Tests ==
|
||||
|
||||
inet_pton() is a newer system call that supports both IPv4 and IPv6. It is a lot more strict about what it deems to be a valid IPv4 address and doesn't support many of the features found in inet_aton() such as support for non- decimal octets, partial numbers of octets, etc.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str_to_int('127', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '127' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0x7f', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '0x7f' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0177', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '0177' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('127.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '127.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0x7f.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '0x7f.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('0177.1', flags=INET_PTON)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '0177.1' is not a valid IPv4 address string!
|
||||
|
||||
>>> str_to_int('127.0.0.1', flags=INET_PTON) == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
@@ -0,0 +1,290 @@
|
||||
=IP version 6 Strategy Module=
|
||||
|
||||
Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
|
||||
{{{
|
||||
|
||||
>>> from netaddr.strategy.ipv6 import *
|
||||
|
||||
}}}
|
||||
|
||||
==Basic Smoke Tests==
|
||||
|
||||
{{{
|
||||
|
||||
>>> b = '0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:1111111111111111:1111111111111110'
|
||||
>>> i = 4294967294
|
||||
>>> t = (0, 0, 0, 0, 0, 0, 0xffff, 0xfffe)
|
||||
>>> s = '::255.255.255.254'
|
||||
>>> p = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xfe'
|
||||
|
||||
>>> bits_to_int(b) == 4294967294
|
||||
True
|
||||
|
||||
>>> int_to_bits(i) == b
|
||||
True
|
||||
|
||||
>>> int_to_str(i)
|
||||
'::255.255.255.254'
|
||||
|
||||
>>> int_to_words(i)
|
||||
(0, 0, 0, 0, 0, 0, 65535, 65534)
|
||||
|
||||
>>> int_to_packed(i)
|
||||
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xfe'
|
||||
|
||||
>>> str_to_int(s) == 4294967294
|
||||
True
|
||||
|
||||
>>> words_to_int(t) == 4294967294
|
||||
True
|
||||
|
||||
>>> words_to_int(list(t)) == 4294967294
|
||||
True
|
||||
|
||||
>>> packed_to_int(p) == 4294967294
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
==More Specific IPv6 Tests==
|
||||
|
||||
IPv6 string address variants that are all equivalent.
|
||||
|
||||
{{{
|
||||
|
||||
>>> i = 42540766411282592856903984951992014763
|
||||
>>> str_to_int('2001:0db8:0000:0000:0000:0000:1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0db8:0000:0000:0000::1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0db8:0:0:0:0:1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0db8:0:0::1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0db8::1428:57ab') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:0DB8:0000:0000:0000:0000:1428:57AB') == i
|
||||
True
|
||||
|
||||
>>> str_to_int('2001:DB8::1428:57AB') == i
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
Intensive IPv6 string address validation testing.
|
||||
|
||||
Positive tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> valid_addrs = (
|
||||
... # RFC 4291
|
||||
... # Long forms.
|
||||
... 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210',
|
||||
... '1080:0:0:0:8:800:200C:417A', # a unicast address
|
||||
... 'FF01:0:0:0:0:0:0:43', # a multicast address
|
||||
... '0:0:0:0:0:0:0:1', # the loopback address
|
||||
... '0:0:0:0:0:0:0:0', # the unspecified addresses
|
||||
...
|
||||
... # Short forms.
|
||||
... '1080::8:800:200C:417A', # a unicast address
|
||||
... 'FF01::43', # a multicast address
|
||||
... '::1', # the loopback address
|
||||
... '::', # the unspecified addresses
|
||||
...
|
||||
... # IPv4 compatible forms.
|
||||
... '::192.0.2.1',
|
||||
... '::ffff:192.0.2.1',
|
||||
... '0:0:0:0:0:0:192.0.2.1',
|
||||
... '0:0:0:0:0:FFFF:192.0.2.1',
|
||||
... '0:0:0:0:0:0:13.1.68.3',
|
||||
... '0:0:0:0:0:FFFF:129.144.52.38',
|
||||
... '::13.1.68.3',
|
||||
... '::FFFF:129.144.52.38',
|
||||
...
|
||||
... # Other tests.
|
||||
... '1::',
|
||||
... '::ffff',
|
||||
... 'ffff::',
|
||||
... 'ffff::ffff',
|
||||
... '0:1:2:3:4:5:6:7',
|
||||
... '8:9:a:b:c:d:e:f',
|
||||
... '0:0:0:0:0:0:0:0',
|
||||
... 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
|
||||
... )
|
||||
|
||||
>>> for addr in valid_addrs:
|
||||
... addr, valid_str(addr)
|
||||
('FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', True)
|
||||
('1080:0:0:0:8:800:200C:417A', True)
|
||||
('FF01:0:0:0:0:0:0:43', True)
|
||||
('0:0:0:0:0:0:0:1', True)
|
||||
('0:0:0:0:0:0:0:0', True)
|
||||
('1080::8:800:200C:417A', True)
|
||||
('FF01::43', True)
|
||||
('::1', True)
|
||||
('::', True)
|
||||
('::192.0.2.1', True)
|
||||
('::ffff:192.0.2.1', True)
|
||||
('0:0:0:0:0:0:192.0.2.1', True)
|
||||
('0:0:0:0:0:FFFF:192.0.2.1', True)
|
||||
('0:0:0:0:0:0:13.1.68.3', True)
|
||||
('0:0:0:0:0:FFFF:129.144.52.38', True)
|
||||
('::13.1.68.3', True)
|
||||
('::FFFF:129.144.52.38', True)
|
||||
('1::', True)
|
||||
('::ffff', True)
|
||||
('ffff::', True)
|
||||
('ffff::ffff', True)
|
||||
('0:1:2:3:4:5:6:7', True)
|
||||
('8:9:a:b:c:d:e:f', True)
|
||||
('0:0:0:0:0:0:0:0', True)
|
||||
('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', True)
|
||||
|
||||
}}}
|
||||
|
||||
Negative tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> invalid_addrs = (
|
||||
... 'g:h:i:j:k:l:m:n', # bad chars.
|
||||
... '0:0:0:0:0:0:0:0:0' # too long,
|
||||
... '', # empty string
|
||||
... # Unexpected types.
|
||||
... [],
|
||||
... (),
|
||||
... {},
|
||||
... True,
|
||||
... False,
|
||||
... )
|
||||
|
||||
>>> for addr in invalid_addrs:
|
||||
... addr, valid_str(addr)
|
||||
('g:h:i:j:k:l:m:n', False)
|
||||
('0:0:0:0:0:0:0:0:0', False)
|
||||
([], False)
|
||||
((), False)
|
||||
({}, False)
|
||||
(True, False)
|
||||
(False, False)
|
||||
|
||||
}}}
|
||||
|
||||
String compaction tests.
|
||||
|
||||
{{{
|
||||
|
||||
>>> valid_addrs = {
|
||||
... # RFC 4291
|
||||
... 'FEDC:BA98:7654:3210:FEDC:BA98:7654:3210' : 'fedc:ba98:7654:3210:fedc:ba98:7654:3210',
|
||||
... '1080:0:0:0:8:800:200C:417A' : '1080::8:800:200c:417a', # a unicast address
|
||||
... 'FF01:0:0:0:0:0:0:43' : 'ff01::43', # a multicast address
|
||||
... '0:0:0:0:0:0:0:1' : '::1', # the loopback address
|
||||
... '0:0:0:0:0:0:0:0' : '::', # the unspecified addresses
|
||||
... }
|
||||
|
||||
>>> for long_form, short_form in valid_addrs.items():
|
||||
... int_val = str_to_int(long_form)
|
||||
... calc_short_form = int_to_str(int_val)
|
||||
... calc_short_form == short_form
|
||||
True
|
||||
True
|
||||
True
|
||||
True
|
||||
True
|
||||
|
||||
}}}
|
||||
|
||||
IPv6 mapped and compatible IPv4 string formatting.
|
||||
|
||||
{{{
|
||||
|
||||
>>> int_to_str(0xffffff)
|
||||
'::0.255.255.255'
|
||||
>>> int_to_str(0xffffffff)
|
||||
'::255.255.255.255'
|
||||
|
||||
>>> int_to_str(0x1ffffffff)
|
||||
'::1:ffff:ffff'
|
||||
|
||||
>>> int_to_str(0xffffffffffff)
|
||||
'::ffff:255.255.255.255'
|
||||
|
||||
>>> int_to_str(0xfffeffffffff)
|
||||
'::fffe:ffff:ffff'
|
||||
|
||||
>>> int_to_str(0xffffffffffff)
|
||||
'::ffff:255.255.255.255'
|
||||
|
||||
>>> int_to_str(0xfffffffffff1)
|
||||
'::ffff:255.255.255.241'
|
||||
|
||||
>>> int_to_str(0xfffffffffffe)
|
||||
'::ffff:255.255.255.254'
|
||||
|
||||
>>> int_to_str(0xffffffffff00)
|
||||
'::ffff:255.255.255.0'
|
||||
|
||||
>>> int_to_str(0xffffffff0000)
|
||||
'::ffff:255.255.0.0'
|
||||
|
||||
>>> int_to_str(0xffffff000000)
|
||||
'::ffff:255.0.0.0'
|
||||
|
||||
>>> int_to_str(0xffff000000)
|
||||
'::ff:ff00:0'
|
||||
|
||||
>>> int_to_str(0xffff00000000)
|
||||
'::ffff:0.0.0.0'
|
||||
|
||||
>>> int_to_str(0x1ffff00000000)
|
||||
'::1:ffff:0:0'
|
||||
|
||||
>>> int_to_str(0xffff00000000)
|
||||
'::ffff:0.0.0.0'
|
||||
|
||||
}}}
|
||||
|
||||
== str_to_int() Behavioural Tests (legacy_mode switch) ==
|
||||
|
||||
The legacy_mode switch on str_to_int() is for interface compatibility only and should not effect the behaviour of this method whether set to True or False.
|
||||
|
||||
{{{
|
||||
|
||||
>>> str_to_int('::127') == 295
|
||||
True
|
||||
|
||||
>>> str_to_int('::0x7f')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '::0x7f' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::0177') == 375
|
||||
True
|
||||
|
||||
>>> str_to_int('::127.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '::127.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::0x7f.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '::0x7f.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::0177.1')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
netaddr.core.AddrFormatError: '::0177.1' is not a valid IPv6 address string!
|
||||
|
||||
>>> str_to_int('::127.0.0.1') == 2130706433
|
||||
True
|
||||
|
||||
}}}
|
||||
Executable
+72
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python
|
||||
#-----------------------------------------------------------------------------
|
||||
# Copyright (c) 2008-2012, David P. D. Moss. All rights reserved.
|
||||
#
|
||||
# Released under the BSD license. See the LICENSE file for details.
|
||||
#-----------------------------------------------------------------------------
|
||||
"""Runs all netaddr unit tests."""
|
||||
|
||||
from os.path import abspath, basename, dirname, join as pathjoin
|
||||
import sys
|
||||
import glob
|
||||
import doctest
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, abspath(pathjoin(dirname(__file__), '..', '..')))
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
def test_suite_all():
|
||||
|
||||
test_dirs = [
|
||||
'ip',
|
||||
'eui',
|
||||
'strategy',
|
||||
'core'
|
||||
]
|
||||
|
||||
base_path = abspath(pathjoin(dirname(__file__), '..'))
|
||||
|
||||
# Select tests based on the version of the Python interpreter.
|
||||
py_ver_dir = '2.x'
|
||||
if sys.version_info[0] == 3:
|
||||
py_ver_dir = '3.x'
|
||||
|
||||
# Gather list of files containing tests.
|
||||
test_files = []
|
||||
for entry in test_dirs:
|
||||
test_path = pathjoin(base_path, "tests", py_ver_dir, entry, "*.txt")
|
||||
files = glob.glob(test_path)
|
||||
test_files.extend(files)
|
||||
|
||||
sys.stdout.write('testdir: %s\n' % '\n'.join(test_files))
|
||||
|
||||
# Add anything to the skiplist that we want to leave out.
|
||||
skiplist = []
|
||||
|
||||
# Drop platform specific tests for other platforms.
|
||||
platform_tests = ['platform_darwin.txt', 'platform_linux2.txt', 'platform_win32.txt']
|
||||
for platform_test in platform_tests:
|
||||
if not sys.platform in platform_test:
|
||||
skiplist.append(platform_test)
|
||||
|
||||
# Exclude any entries from the skip list.
|
||||
test_files = [t for t in test_files if basename(t) not in skiplist]
|
||||
|
||||
# Build and return a complete unittest test suite.
|
||||
suite = unittest.TestSuite()
|
||||
|
||||
for test_file in test_files:
|
||||
doctest_suite = doctest.DocFileSuite(test_file,
|
||||
optionflags=doctest.ELLIPSIS, module_relative=False)
|
||||
suite.addTest(doctest_suite)
|
||||
|
||||
return suite
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
def run():
|
||||
runner = unittest.TextTestRunner()
|
||||
runner.run(test_suite_all())
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
Reference in New Issue
Block a user