summaryrefslogtreecommitdiff
path: root/wiki/ExampleUsage-1x.wiki
blob: 3a978ac11ecef495b47b1953d999289c24fe7456 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

Examples of the functionality of the 1.x version of the ipaddr library. Documentation for the current version (2.x) will be coming shortly.

A lot of the functionality of the IPv4 class is derived from the BaseIP class. The same is true for the IPv6 class. that means that all of the functionality you see here is exactly the same when dealing with IPv6 addresses.

=== IPv4 ===
_using ipaddr with IPv4 addresses_

Start by creating your address object.
You can create an IPv4 object with:

netmask
{{{
>>> addr = ipaddr.IPv4('1.1.1.1/255.255.255.0')
>>> addr
IPv4('1.1.1.1/24')
}}}

hostmask
{{{
>>> addr = ipaddr.IPv4('1.1.1.1/0.0.0.255')
>>> addr
IPv4('1.1.1.1/24')
}}}

prefix length 
{{{
>>> addr = ipaddr.IPv4('1.1.1.1/24')
>>> addr
IPv4('1.1.1.1/24')
}}}

nothing (nothing implies a /32 netmask)
{{{
>>> addr = ipaddr.IPv4('1.1.1.1')
>>> addr
IPv4('1.1.1.1/32')
}}}

Many of the standard Python operations are supported
{{{
# comparisons
>>> ipaddr.IPv4('1.1.1.1') == ipaddr.IPv4('1.1.1.2')
False
>>> ipaddr.IPv4('1.1.1.1') < ipaddr.IPv4('1.1.1.2')
True

# list inclusion
>>> a = ipaddr.IPv4('1.1.1.1')
>>> b = ipaddr.IPv4('1.1.1.2')
>>> c = ipaddr.IPv4('1.1.1.3')
>>> a in [a, b, c]
True

# inclusion
>>> ipaddr.IPv4('1.1.1.1') in ipaddr.IPv4("1.0.0.0/8")
True

# sorting
>>> a = ipaddr.IPv4('1.1.1.10')
>>> b = ipaddr.IPv4('1.10.1.10')
>>> c = ipaddr.IPv4('1.1.10.10')
>>> d = ipaddr.IPv4('1.1.1.1')
>>> sorted([a, b, c, d])
[IPv4('1.1.1.1/32'), IPv4('1.1.1.10/32'), IPv4('1.1.10.10/32'), IPv4('1.10.1.10/32')]

# str
>>> str(ipaddr.IP('1.2.3.4'))
'1.2.3.4/32'

# int/hex.
>>> int(ipaddr.IP('1.2.3.4'))
16909060
>>> hex(ipaddr.IP('1.2.3.4'))
'0x1020304'
}}}

Additionally, there are quite a few network-specific features available to ipaddr.
{{{
>>> ipaddr.IPv4('10.0.0.0/8').Supernet()
IPv4('10.0.0.0/7')

>>> ipaddr.IPv4('10.0.0.0/8').Subnet()
[IPv4('10.0.0.0/9'), IPv4('10.128.0.0/9')]

>>> ipaddr.IPv4('10.0.0.0/8').Subnet(prefixlen_diff=2) # this returns networks with a prefix length of /10
[IPv4('10.0.0.0/10'), IPv4('10.64.0.0/10'), 
 IPv4('10.128.0.0/10'), IPv4('10.192.0.0/10')]

# AddressExclude removes an address from a superblock.
>>> ipaddr.IPv4('10.0.0.0/24').AddressExclude(ipaddr.IPv4('10.0.0.0/32'))
[IPv4('10.0.0.1/32'), IPv4('10.0.0.2/31'), 
 IPv4('10.0.0.4/30'), IPv4('10.0.0.8/29'),
 IPv4('10.0.0.16/28'), IPv4('10.0.0.32/27'), 
 IPv4('10.0.0.64/26'), IPv4('10.0.0.128/25')]

}}}

=== IPv6 ===

IPv6 methods are exactly the same as their IPv4 counterparts.  this is because a lot of the functionality for both the IPv4 and IPv6 classes is inherited from the BaseIP class. For example:

{{{
>>> addr = ipaddr.IPv6('ffff::1/120')
>>> addr
IPv6('ffff::1/120')
>>> addr.prefixlen
120
>>> addr == ipaddr.IPv6('ffff::2/120')
False
>>> addr > ipaddr.IPv6('ffff::2/120')
False
>>> addr < ipaddr.IPv6('ffff::2/120')
True
>>> addr in ipaddr.IPv6('ffff::1/119')
True
>>> hex(addr)
'0xFFFF0000000000000000000000000001L'
>>> int(addr)
340277174624079928635746076935438991361L
}}}


As always, consult your local help(ipaddr) pydoc documentation with questions.