netwatch.py/netaddr-0.7.10/netaddr/tests/3.x/ip/subnet.txt

109 lines
2.1 KiB
Plaintext

=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')
}}}