annotate Lib/ipaddr.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 510ffc5db232
children 4427859d817a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1 # Copyright 2007 Google Inc.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
2 # Licensed to PSF under a Contributor Agreement.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
3 #
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
4 # Licensed under the Apache License, Version 2.0 (the "License");
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
5 # you may not use this file except in compliance with the License.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6 # You may obtain a copy of the License at
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
7 #
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
8 # http://www.apache.org/licenses/LICENSE-2.0
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
9 #
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
10 # Unless required by applicable law or agreed to in writing, software
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
11 # distributed under the License is distributed on an "AS IS" BASIS,
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
13 # implied. See the License for the specific language governing
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
14 # permissions and limitations under the License.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
15 #
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
16 # See also: http://code.google.com/p/ipaddr-py/
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
17
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
18 """An IPv4/IPv6 manipulation library in Python.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
19
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
20 This library is used to create/poke/manipulate IPv4 and IPv6 addresses
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
21 and prefixes.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
22
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
23 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
24
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
25 __version__ = '1.0.2'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
26
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
27 import struct
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
28
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
29
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
30 class Error(Exception):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
31
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
32 """Base class for exceptions."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
33
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
34
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
35 class IPTypeError(Error):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
36
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
37 """Tried to perform a v4 action on v6 object or vice versa."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
38
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
39
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
40 class IPAddressExclusionError(Error):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
41
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
42 """An Error we should never see occurred in address exclusion."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
43
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
44
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
45 class IPv4IpValidationError(Error):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
46
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
47 """Raised when an IPv4 address is invalid."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
48
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
49 def __init__(self, ip):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
50 Error.__init__(self)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
51 self.ip = ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
52
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
53 def __str__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
54 return repr(self.ip) + ' is not a valid IPv4 address'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
55
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
56
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
57 class IPv4NetmaskValidationError(Error):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
58
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
59 """Raised when a netmask is invalid."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
60
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
61 def __init__(self, netmask):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
62 Error.__init__(self)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
63 self.netmask = netmask
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
64
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
65 def __str__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
66 return repr(self.netmask) + ' is not a valid IPv4 netmask'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
67
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
68
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
69 class IPv6IpValidationError(Error):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
70
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
71 """Raised when an IPv6 address is invalid."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
72
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
73 def __init__(self, ip):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
74 Error.__init__(self)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
75 self.ip = ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
76
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
77 def __str__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
78 return repr(self.ip) + ' is not a valid IPv6 address'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
79
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
80
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
81 class IPv6NetmaskValidationError(Error):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
82
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
83 """Raised when an IPv6 netmask is invalid."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
84
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
85 def __init__(self, netmask):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
86 Error.__init__(self)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
87 self.netmask = netmask
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
88
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
89 def __str__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
90 return repr(self.netmask) + ' is not a valid IPv6 netmask'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
91
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
92
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
93 class PrefixlenDiffInvalidError(Error):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
94
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
95 """Raised when Sub/Supernets is called with a bad prefixlen_diff."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
96
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
97 def __init__(self, error_str):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
98 Error.__init__(self)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
99 self.error_str = error_str
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
100
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
101
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
102 def IP(ipaddr):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
103 """Take an IP string/int and return an object of the correct type.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
104
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
105 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
106 ipaddr: A string or integer, the IP address. Either IPv4 or
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
107 IPv6 addresses may be supplied; integers less than 2**32 will
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
108 be considered to be IPv4.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
109
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
110 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
111 An IPv4 or IPv6 object.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
112
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
113 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
114 ValueError: if the string passed isn't either a v4 or a v6
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
115 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
116
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
117 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
118
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
119 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
120 return IPv4(ipaddr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
121 except (IPv4IpValidationError, IPv4NetmaskValidationError):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
122 pass
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
123
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
124 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
125 return IPv6(ipaddr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
126 except (IPv6IpValidationError, IPv6NetmaskValidationError):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
127 pass
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
128
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
129 raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
130 ipaddr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
131
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
132
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
133 def _collapse_address_list_recursive(addresses):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
134 """Loops through the addresses, collapsing concurrent netblocks.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
135
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
136 Example:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
137
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
138 ip1 = IPv4('1.1.0.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
139 ip2 = IPv4('1.1.1.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
140 ip3 = IPv4('1.1.2.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
141 ip4 = IPv4('1.1.3.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
142 ip5 = IPv4('1.1.4.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
143 ip6 = IPv4('1.1.0.1/22')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
144
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
145 _collapse_address_list_recursive([ip1, ip2, ip3, ip4, ip5, ip6]) ->
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
146 [IPv4('1.1.0.0/22'), IPv4('1.1.4.0/24')]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
147
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
148 This shouldn't be called directly; it is called via
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
149 collapse_address_list([]).
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
150
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
151 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
152 addresses: A list of IPv4 or IPv6 objects.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
153
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
154 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
155 A list of IPv4 or IPv6 objects depending on what we were passed.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
156
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
157 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
158 ret_array = []
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
159 optimized = False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
160
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
161 for cur_addr in addresses:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
162 if not ret_array:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
163 ret_array.append(cur_addr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
164 continue
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
165 if cur_addr in ret_array[-1]:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
166 optimized = True
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
167 elif cur_addr == ret_array[-1].supernet().subnet()[1]:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
168 ret_array.append(ret_array.pop().supernet())
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
169 optimized = True
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
170 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
171 ret_array.append(cur_addr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
172
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
173 if optimized:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
174 return _collapse_address_list_recursive(ret_array)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
175
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
176 return ret_array
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
177
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
178
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
179 def collapse_address_list(addresses):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
180 """Collapse a list of IP objects.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
181
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
182 Example:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
183 collapse_address_list([IPv4('1.1.0.0/24'), IPv4('1.1.1.0/24')]) ->
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
184 [IPv4('1.1.0.0/23')]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
185
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
186 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
187 addresses: A list of IPv4 or IPv6 objects.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
188
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
189 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
190 A list of IPv4 or IPv6 objects depending on what we were passed.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
191
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
192 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
193 return _collapse_address_list_recursive(
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
194 sorted(addresses, key=BaseIP._get_networks_key))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
195
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
196
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
197 class BaseIP(object):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
198
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
199 """A generic IP object.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
200
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
201 This IP class contains most of the methods which are used by
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
202 the IPv4 and IPv6 classes.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
203
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
204 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
205
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
206 def __getitem__(self, n):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
207 if n >= 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
208 if self.network + n > self.broadcast:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
209 raise IndexError
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
210 return self._string_from_ip_int(self.network + n)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
211 else:
53932
510ffc5db232 Fix an off by one error on negative indexs to __getitem__
Gregory P. Smith <greg@mad-scientist.com>
parents: 53703
diff changeset
212 n += 1
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
213 if self.broadcast + n < self.network:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
214 raise IndexError
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
215 return self._string_from_ip_int(self.broadcast + n)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
216
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
217 def __lt__(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
218 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
219 return (self.version < other.version
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
220 or self.ip < other.ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
221 or self.netmask < other.netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
222 except AttributeError:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
223 return NotImplemented
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
224
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
225 def __gt__(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
226 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
227 return (self.version > other.version
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
228 or self.ip > other.ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
229 or self.netmask > other.netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
230 except AttributeError:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
231 return NotImplemented
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
232
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
233 def __eq__(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
234 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
235 return (self.version == other.version
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
236 and self.ip == other.ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
237 and self.netmask == other.netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
238 except AttributeError:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
239 return NotImplemented
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
240
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
241 def __ne__(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
242 eq = self.__eq__(other)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
243 if eq is NotImplemented:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
244 return NotImplemented
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
245 return not eq
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
246
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
247 def __le__(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
248 gt = self.__gt__(other)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
249 if gt is NotImplemented:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
250 return NotImplemented
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
251 return not gt
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
252
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
253 def __ge__(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
254 lt = self.__lt__(other)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
255 if lt is NotImplemented:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
256 return NotImplemented
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
257 return not lt
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
258
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
259 def __repr__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
260 return '%s(%r)' % (self.__class__.__name__, str(self))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
261
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
262 def __index__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
263 return self.ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
264
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
265 def __int__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
266 return self.ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
267
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
268 def __hex__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
269 return hex(int(self))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
270
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
271 def address_exclude(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
272 """Remove an address from a larger block.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
273
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
274 For example:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
275
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
276 addr1 = IP('10.1.1.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
277 addr2 = IP('10.1.1.0/26')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
278 addr1.address_exclude(addr2) =
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
279 [IP('10.1.1.64/26'), IP('10.1.1.128/25')]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
280
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
281 or IPv6:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
282
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
283 addr1 = IP('::1/32')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
284 addr2 = IP('::1/128')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
285 addr1.address_exclude(addr2) = [IP('::0/128'),
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
286 IP('::2/127'),
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
287 IP('::4/126'),
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
288 IP('::8/125'),
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
289 ...
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
290 IP('0:0:8000::/33')]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
291
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
292 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
293 other: An IP object of the same type.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
294
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
295 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
296 A sorted list of IP objects addresses which is self minus
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
297 other.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
298
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
299 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
300 IPTypeError: If self and other are of difffering address
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
301 versions.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
302 IPAddressExclusionError: There was some unknown error in the
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
303 address exclusion process. This likely points to a bug
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
304 elsewhere in this code.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
305 ValueError: If other is not completely contained by self.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
306
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
307 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
308 if not self.version == other.version:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
309 raise IPTypeError("%s and %s aren't of the same version" % (
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
310 str(self), str(other)))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
311
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
312 if other not in self:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
313 raise ValueError('%s not contained in %s' % (str(other),
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
314 str(self)))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
315
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
316 ret_addrs = []
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
317
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
318 # Make sure we're comparing the network of other.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
319 other = IP(other.network_ext + '/' + str(other.prefixlen))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
320
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
321 s1, s2 = self.subnet()
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
322 while s1 != other and s2 != other:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
323 if other in s1:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
324 ret_addrs.append(s2)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
325 s1, s2 = s1.subnet()
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
326 elif other in s2:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
327 ret_addrs.append(s1)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
328 s1, s2 = s2.subnet()
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
329 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
330 # If we got here, there's a bug somewhere.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
331 raise IPAddressExclusionError('Error performing exclusion: '
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
332 's1: %s s2: %s other: %s' %
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
333 (str(s1), str(s2), str(other)))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
334 if s1 == other:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
335 ret_addrs.append(s2)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
336 elif s2 == other:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
337 ret_addrs.append(s1)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
338 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
339 # If we got here, there's a bug somewhere.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
340 raise IPAddressExclusionError('Error performing exclusion: '
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
341 's1: %s s2: %s other: %s' %
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
342 (str(s1), str(s2), str(other)))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
343
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
344 return sorted(ret_addrs, key=BaseIP._get_networks_key)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
345
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
346 def compare_networks(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
347 """Compare two IP objects.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
348
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
349 This is only concerned about the comparison of the integer
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
350 representation of the network addresses. This means that the
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
351 host bits aren't considered at all in this method. If you want
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
352 to compare host bits, you can easily enough do a
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
353 'HostA.ip < HostB.ip'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
354
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
355 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
356 other: An IP object.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
357
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
358 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
359 If the IP versions of self and other are the same, returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
360
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
361 -1 if self < other:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
362 eg: IPv4('1.1.1.0/24') < IPv4('1.1.2.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
363 IPv6('1080::200C:417A') < IPv6('1080::200B:417B')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
364 0 if self == other
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
365 eg: IPv4('1.1.1.1/24') == IPv4('1.1.1.2/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
366 IPv6('1080::200C:417A/96') == IPv6('1080::200C:417B/96')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
367 1 if self > other
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
368 eg: IPv4('1.1.1.0/24') > IPv4('1.1.0.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
369 IPv6('1080::1:200C:417A/112') >
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
370 IPv6('1080::0:200C:417A/112')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
371
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
372 If the IP versions of self and other are different, returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
373
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
374 -1 if self.version < other.version
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
375 eg: IPv4('10.0.0.1/24') < IPv6('::1/128')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
376 1 if self.version > other.version
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
377 eg: IPv6('::1/128') > IPv4('255.255.255.0/24')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
378
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
379 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
380 if self.version < other.version:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
381 return -1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
382 if self.version > other.version:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
383 return 1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
384 # self.version == other.version below here:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
385 if self.network < other.network:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
386 return -1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
387 if self.network > other.network:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
388 return 1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
389 # self.network == other.network below here:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
390 if self.netmask < other.netmask:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
391 return -1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
392 if self.netmask > other.netmask:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
393 return 1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
394 # self.network == other.network and self.netmask == other.netmask
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
395 return 0
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
396
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
397 def _get_networks_key(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
398 """Network-only key function.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
399
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
400 Returns an object that identifies this address' network and
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
401 netmask. This function is a suitable "key" argument for sorted()
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
402 and list.sort().
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
403
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
404 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
405 return (self.version, self.network, self.netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
406
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
407 prefixlen = property(
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
408 fget=lambda self: self._prefixlen,
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
409 fset=lambda self, prefixlen: self._set_prefix(prefixlen))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
410
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
411 def __str__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
412 return '%s/%s' % (self._string_from_ip_int(self.ip),
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
413 str(self.prefixlen))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
414
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
415 def __hash__(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
416 return hash(self.ip ^ self.netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
417
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
418 def __contains__(self, other):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
419 return self.network <= other.ip and self.broadcast >= other.broadcast
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
420
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
421 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
422 def ip_ext(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
423 """Dotted decimal or colon string version of the IP address."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
424 return self._string_from_ip_int(self.ip)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
425
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
426 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
427 def ip_ext_full(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
428 """Canonical string version of the IP address."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
429 return self.ip_ext
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
430
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
431 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
432 def broadcast(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
433 """Integer representation of the broadcast address."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
434 return self.ip | self.hostmask
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
435
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
436 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
437 def broadcast_ext(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
438 """Dotted decimal or colon string version of the broadcast."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
439 return self._string_from_ip_int(self.broadcast)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
440
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
441 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
442 def hostmask(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
443 """Integer representation of the hostmask."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
444 return self.netmask ^ self._ALL_ONES
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
445
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
446 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
447 def hostmask_ext(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
448 """Dotted decimal or colon string version of the hostmask."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
449 return self._string_from_ip_int(self.hostmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
450
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
451 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
452 def network(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
453 """Integer representation of the network."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
454 return self.ip & self.netmask
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
455
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
456 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
457 def network_ext(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
458 """Dotted decimal or colon string version of the network."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
459 return self._string_from_ip_int(self.network)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
460
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
461 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
462 def netmask_ext(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
463 """Dotted decimal or colon string version of the netmask."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
464 return self._string_from_ip_int(self.netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
465
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
466 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
467 def numhosts(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
468 """Number of hosts in the current subnet."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
469 return self.broadcast - self.network + 1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
470
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
471 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
472 def version(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
473 raise NotImplementedError('BaseIP has no version')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
474
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
475 def _ip_int_from_prefix(self, prefixlen=None):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
476 """Turn the prefix length netmask into a int for comparison.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
477
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
478 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
479 prefixlen: An integer, the prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
480
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
481 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
482 An integer.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
483
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
484 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
485 if not prefixlen and prefixlen != 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
486 prefixlen = self.prefixlen
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
487 return self._ALL_ONES ^ (self._ALL_ONES >> prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
488
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
489 def _prefix_from_ip_int(self, ip_int, mask=32):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
490 """Return prefix length from the decimal netmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
491
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
492 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
493 ip_int: An integer, the IP address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
494 mask: The netmask. Defaults to 32.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
495
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
496 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
497 An integer, the prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
498
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
499 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
500 while mask:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
501 if ip_int & 1 == 1:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
502 break
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
503 ip_int >>= 1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
504 mask -= 1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
505
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
506 return mask
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
507
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
508 def _ip_string_from_prefix(self, prefixlen=None):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
509 """Turn a prefix length into a dotted decimal string.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
510
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
511 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
512 prefixlen: An integer, the netmask prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
513
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
514 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
515 A string, the dotted decimal netmask string.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
516
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
517 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
518 if not prefixlen:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
519 prefixlen = self.prefixlen
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
520 return self._string_from_ip_int(self._ip_int_from_prefix(prefixlen))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
521
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
522
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
523 class IPv4(BaseIP):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
524
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
525 """This class represents and manipulates 32-bit IPv4 addresses.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
526
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
527 Attributes: [examples for IPv4('1.2.3.4/27')]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
528 .ip: 16909060
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
529 .ip_ext: '1.2.3.4'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
530 .ip_ext_full: '1.2.3.4'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
531 .network: 16909056L
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
532 .network_ext: '1.2.3.0'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
533 .hostmask: 31L (0x1F)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
534 .hostmask_ext: '0.0.0.31'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
535 .broadcast: 16909087L (0x102031F)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
536 .broadcast_ext: '1.2.3.31'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
537 .netmask: 4294967040L (0xFFFFFFE0)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
538 .netmask_ext: '255.255.255.224'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
539 .prefixlen: 27
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
540
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
541 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
542
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
543 # Equivalent to 255.255.255.255 or 32 bits of 1's.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
544 _ALL_ONES = 0xffffffff
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
545 _version = 4
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
546
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
547 def __init__(self, ipaddr):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
548 """Instantiate a new IPv4 object.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
549
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
550 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
551 ipaddr: A string or integer representing the IP [& network].
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
552 '192.168.1.1/32'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
553 '192.168.1.1/255.255.255.255'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
554 '192.168.1.1/0.0.0.255'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
555 '192.168.1.1'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
556 are all functionally the same in IPv4. That is to say,
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
557 failing to provide a subnetmask will create an object with
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
558 a mask of /32. A netmask of '255.255.255.255' is assumed
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
559 to be /32 and '0.0.0.0' is assumed to be /0, even though
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
560 other netmasks can be expressed both as host- and
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
561 net-masks. (255.0.0.0 == 0.255.255.255)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
562
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
563 Additionally, an integer can be passed, so
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
564 IPv4('192.168.1.1') == IPv4(3232235777).
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
565 or, more generally
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
566 IPv4(IPv4('192.168.1.1').ip) == IPv4('192.168.1.1')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
567
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
568 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
569 IPv4IpValidationError: If ipaddr isn't a valid IPv4 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
570 IPv4NetmaskValidationError: If the netmask isn't valid for
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
571 an IPv4 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
572
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
573 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
574 BaseIP.__init__(self)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
575
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
576 # Efficient constructor from integer.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
577 if isinstance(ipaddr, int) or isinstance(ipaddr, long):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
578 self.ip = ipaddr
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
579 self._prefixlen = 32
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
580 self.netmask = self._ALL_ONES
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
581 if ipaddr < 0 or ipaddr > self._ALL_ONES:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
582 raise IPv4IpValidationError(ipaddr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
583 return
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
584
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
585 # Assume input argument to be string or any object representation
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
586 # which converts into a formatted IP prefix string.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
587 addr = str(ipaddr).split('/')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
588
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
589 if len(addr) > 2:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
590 raise IPv4IpValidationError(ipaddr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
591
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
592 if not self._is_valid_ip(addr[0]):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
593 raise IPv4IpValidationError(addr[0])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
594
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
595 self.ip = self._ip_int_from_string(addr[0])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
596
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
597 if len(addr) == 2:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
598 mask = addr[1].split('.')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
599 if len(mask) == 4:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
600 # We have dotted decimal netmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
601 if not self._is_valid_netmask(addr[1]):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
602 raise IPv4NetmaskValidationError(addr[1])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
603 if self._is_hostmask(addr[1]):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
604 self.netmask = (
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
605 self._ip_int_from_string(addr[1]) ^ self._ALL_ONES)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
606 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
607 self.netmask = self._ip_int_from_string(addr[1])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
608 self._prefixlen = self._prefix_from_ip_int(self.netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
609 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
610 # We have a netmask in prefix length form.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
611 if not self._is_valid_netmask(addr[1]):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
612 raise IPv4NetmaskValidationError(addr[1])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
613 self._prefixlen = int(addr[1])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
614 self.netmask = self._ip_int_from_prefix(self._prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
615 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
616 self._prefixlen = 32
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
617 self.netmask = self._ip_int_from_prefix(self._prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
618
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
619 def _set_prefix(self, prefixlen):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
620 """Change the prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
621
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
622 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
623 prefixlen: An integer, the new prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
624
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
625 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
626 IPv4NetmaskValidationError: If prefixlen is out of bounds.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
627
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
628 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
629 if not 0 <= prefixlen <= 32:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
630 raise IPv4NetmaskValidationError(prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
631 self._prefixlen = prefixlen
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
632 self.netmask = self._ip_int_from_prefix(self._prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
633
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
634 def subnet(self, prefixlen_diff=1):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
635 """The subnets which join to make the current subnet.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
636
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
637 In the case that self contains only one IP
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
638 (self._prefixlen == 32), return a list with just ourself.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
639
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
640 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
641 prefixlen_diff: An integer, the amount the prefix length
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
642 should be increased by. Given a /24 network and a
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
643 prefixlen_diff of 3, for example, 8 subnets of size /27
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
644 will be returned. The default value of 1 splits the
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
645 current network into two halves.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
646
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
647 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
648 A list of IPv4 objects.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
649
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
650 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
651 PrefixlenDiffInvalidError: The prefixlen_diff is too small
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
652 or too large.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
653
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
654 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
655 if self._prefixlen == 32:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
656 return [self]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
657
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
658 if prefixlen_diff < 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
659 raise PrefixlenDiffInvalidError('prefix length diff must be > 0')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
660 new_prefixlen = self.prefixlen + prefixlen_diff
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
661
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
662 if not self._is_valid_netmask(str(new_prefixlen)):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
663 raise PrefixlenDiffInvalidError(
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
664 'prefix length diff %d is invalid for netblock %s' % (
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
665 new_prefixlen, str(self)))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
666
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
667 first = IPv4(
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
668 self._string_from_ip_int(self.network) + '/' +
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
669 str(self._prefixlen + prefixlen_diff))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
670 subnets = [first]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
671 current = first
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
672 while True:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
673 broadcast = current.broadcast
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
674 if broadcast == self.broadcast:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
675 break
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
676 current = IPv4(self._string_from_ip_int(broadcast + 1) + '/' +
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
677 str(new_prefixlen))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
678 subnets.append(current)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
679
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
680 return subnets
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
681
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
682 def supernet(self, prefixlen_diff=1):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
683 """The supernet containing the current network.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
684
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
685 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
686 prefixlen_diff: An integer, the amount the prefix length of
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
687 the network should be decreased by. For example, given a
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
688 /24 network and a prefixlen_diff of 3, a supernet with a
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
689 /21 netmask is returned.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
690
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
691 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
692 An IPv4 object.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
693
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
694 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
695 PrefixlenDiffInvalidError: If
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
696 self.prefixlen - prefixlen_diff < 0. I.e., you have a
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
697 negative prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
698
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
699 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
700 if self.prefixlen == 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
701 return self
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
702 if self.prefixlen - prefixlen_diff < 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
703 raise PrefixlenDiffInvalidError(
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
704 'current prefixlen is %d, cannot have a prefixlen_diff of %d' %
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
705 (self.prefixlen, prefixlen_diff))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
706 return IPv4(self.ip_ext + '/' + str(self.prefixlen - prefixlen_diff))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
707
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
708 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
709 def is_private(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
710 """Test if this address is allocated for private networks.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
711
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
712 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
713 A boolean, True if the address is reserved per RFC 1918.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
714
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
715 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
716 for network in _IPV4_RFC1918_NETWORKS:
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
717 if self in network:
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
718 return True
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
719 return False
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
720
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
721 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
722 def is_multicast(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
723 """Test if the address is reserved for multicast use.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
724
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
725 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
726 A boolean, True if the address is multicast.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
727 See RFC 3171 for details.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
728
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
729 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
730 return self in _IPV4_RFC3171_MULTICAST
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
731
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
732 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
733 def is_loopback(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
734 """Test if the address is a loopback adddress.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
735
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
736 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
737 A boolean, True if the address is a loopback per RFC 3330.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
738
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
739 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
740 return self in _IPV4_RFC3330_LOOPBACK
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
741
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
742 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
743 def is_link_local(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
744 """Test if the address is reserved for link-local.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
745
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
746 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
747 A boolean, True if the address is link-local per RFC 3927.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
748
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
749 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
750 return self in _IPV4_RFC3927_LINK_LOCAL
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
751
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
752 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
753 def version(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
754 return self._version
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
755
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
756 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
757 def packed(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
758 """The binary representation of this address."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
759 return struct.pack('!I', self.ip)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
760
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
761 def _is_hostmask(self, ip_str):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
762 """Test if the IP string is a hostmask (rather than a netmask).
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
763
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
764 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
765 ip_str: A string, the potential hostmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
766
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
767 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
768 A boolean, True if the IP string is a hostmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
769
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
770 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
771 parts = [int(x) for x in ip_str.split('.')]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
772 if parts[0] < parts[-1]:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
773 return True
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
774 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
775
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
776 def _ip_int_from_string(self, ip_str):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
777 """Turn the given IP string into an integer for comparison.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
778
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
779 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
780 ip_str: A string, the IP address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
781
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
782 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
783 The IP address as an integer.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
784
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
785 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
786 packed_ip = 0
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
787 for oc in ip_str.split('.'):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
788 packed_ip = (packed_ip << 8) | int(oc)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
789 return packed_ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
790
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
791 def _string_from_ip_int(self, ip_int):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
792 """Turns a 32-bit integer into dotted decimal notation.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
793
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
794 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
795 ip_int: An integer, the IP address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
796
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
797 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
798 The IP address as a string in dotted decimal notation.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
799
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
800 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
801 octets = []
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
802 for _ in xrange(4):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
803 octets.insert(0, str(ip_int & 0xFF))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
804 ip_int >>= 8
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
805 return '.'.join(octets)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
806
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
807 def _is_valid_ip(self, ip_str):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
808 """Validate the dotted decimal notation IP/netmask string.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
809
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
810 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
811 ip_str: A string, the IP address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
812
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
813 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
814 A boolean, True if the string is a valid dotted decimal IP
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
815 string.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
816
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
817 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
818 octets = ip_str.split('.')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
819 if len(octets) == 1:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
820 # We have an integer rather than a dotted decimal IP.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
821 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
822 return int(ip_str) >= 0 and int(ip_str) <= self._ALL_ONES
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
823 except ValueError:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
824 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
825
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
826 if len(octets) != 4:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
827 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
828
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
829 for octet in octets:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
830 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
831 if not 0 <= int(octet) <= 255:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
832 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
833 except ValueError:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
834 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
835 return True
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
836
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
837 def _is_valid_netmask(self, netmask):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
838 """Verify that the netmask is valid.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
839
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
840 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
841 netmask: A string, either a prefix or dotted decimal
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
842 netmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
843
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
844 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
845 A boolean, True if the prefix represents a valid IPv4
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
846 netmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
847
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
848 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
849 if len(netmask.split('.')) == 4:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
850 return self._is_valid_ip(netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
851 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
852 netmask = int(netmask)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
853 except ValueError:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
854 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
855 return 0 <= netmask <= 32
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
856
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
857
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
858 class IPv6(BaseIP):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
859
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
860 """This class respresents and manipulates 128-bit IPv6 addresses.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
861
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
862 Attributes: [examples for IPv6('2001:658:22A:CAFE:200::1/64')]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
863 .ip: 42540616829182469433547762482097946625L
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
864 .ip_ext: '2001:658:22a:cafe:200::1'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
865 .ip_ext_full: '2001:0658:022a:cafe:0200:0000:0000:0001'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
866 .network: 42540616829182469433403647294022090752L
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
867 .network_ext: '2001:658:22a:cafe::'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
868 .hostmask: 18446744073709551615L
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
869 .hostmask_ext: '::ffff:ffff:ffff:ffff'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
870 .broadcast: 42540616829182469451850391367731642367L
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
871 .broadcast_ext: '2001:658:22a:cafe:ffff:ffff:ffff:ffff'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
872 .netmask: 340282366920938463444927863358058659840L
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
873 .netmask_ext: 64
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
874 .prefixlen: 64
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
875
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
876 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
877
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
878 _ALL_ONES = (2**128) - 1
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
879 _version = 6
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
880
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
881 def __init__(self, ipaddr):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
882 """Instantiate a new IPv6 object.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
883
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
884 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
885 ipaddr: A string or integer representing the IP or the IP
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
886 and prefix/netmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
887 '2001:4860::/128'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
888 '2001:4860:0000:0000:0000:0000:0000:0000/128'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
889 '2001:4860::'
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
890 are all functionally the same in IPv6. That is to say,
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
891 failing to provide a subnetmask will create an object with
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
892 a mask of /128.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
893
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
894 Additionally, an integer can be passed, so
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
895 IPv6('2001:4860::') ==
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
896 IPv6(42541956101370907050197289607612071936L).
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
897 or, more generally
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
898 IPv6(IPv6('2001:4860::').ip) == IPv6('2001:4860::')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
899
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
900 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
901 IPv6IpValidationError: If ipaddr isn't a valid IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
902 IPv6NetmaskValidationError: If the netmask isn't valid for
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
903 an IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
904
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
905 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
906 BaseIP.__init__(self)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
907
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
908 # Efficient constructor from integer.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
909 if isinstance(ipaddr, long) or isinstance(ipaddr, int):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
910 self.ip = ipaddr
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
911 self._prefixlen = 128
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
912 self.netmask = self._ALL_ONES
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
913 if ipaddr < 0 or ipaddr > self._ALL_ONES:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
914 raise IPv6IpValidationError(ipaddr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
915 return
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
916
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
917 # Assume input argument to be string or any object representation
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
918 # which converts into a formatted IP prefix string.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
919 addr_str = str(ipaddr)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
920 if not addr_str:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
921 raise IPv6IpValidationError('')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
922 addr = addr_str.split('/')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
923 if len(addr) > 1:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
924 if self._is_valid_netmask(addr[1]):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
925 self._prefixlen = int(addr[1])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
926 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
927 raise IPv6NetmaskValidationError(addr[1])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
928 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
929 self._prefixlen = 128
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
930
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
931 self.netmask = self._ip_int_from_prefix(self._prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
932
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
933 if not self._is_valid_ip(addr[0]):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
934 raise IPv6IpValidationError(addr[0])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
935
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
936 self.ip = self._ip_int_from_string(addr[0])
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
937
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
938 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
939 def ip_ext_full(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
940 """Returns the expanded version of the IPv6 string."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
941 return self._explode_shorthand_ip_string(self.ip_ext)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
942
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
943 def _set_prefix(self, prefixlen):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
944 """Change the prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
945
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
946 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
947 prefixlen: An integer, the new prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
948
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
949 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
950 IPv6NetmaskValidationError: If prefixlen is out of bounds.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
951
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
952 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
953 if not 0 <= prefixlen <= 128:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
954 raise IPv6NetmaskValidationError(prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
955 self._prefixlen = prefixlen
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
956 self.netmask = self._ip_int_from_prefix(self.prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
957
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
958 def subnet(self, prefixlen_diff=1):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
959 """The subnets which join to make the current subnet.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
960
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
961 In the case that self contains only one IP
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
962 (self._prefixlen == 128), return a list with just ourself.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
963
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
964 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
965 prefixlen_diff: An integer, the amount the prefix length
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
966 should be increased by.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
967
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
968 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
969 A list of IPv6 objects.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
970
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
971 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
972 PrefixlenDiffInvalidError: The prefixlen_diff is too small
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
973 or too large.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
974
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
975 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
976 # Preserve original functionality (return [self] if
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
977 # self.prefixlen == 128).
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
978 if self.prefixlen == 128:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
979 return [self]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
980
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
981 if prefixlen_diff < 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
982 raise PrefixlenDiffInvalidError('Prefix length diff must be > 0')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
983 new_prefixlen = self.prefixlen + prefixlen_diff
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
984 if not self._is_valid_netmask(str(new_prefixlen)):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
985 raise PrefixlenDiffInvalidError(
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
986 'Prefix length diff %d is invalid for netblock %s' % (
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
987 new_prefixlen, str(self)))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
988 first = IPv6(
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
989 self._string_from_ip_int(self.network) + '/' +
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
990 str(self._prefixlen + prefixlen_diff))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
991 subnets = [first]
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
992 current = first
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
993 while True:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
994 broadcast = current.broadcast
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
995 if current.broadcast == self.broadcast:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
996 break
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
997 current = IPv6(self._string_from_ip_int(broadcast + 1) + '/' +
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
998 str(new_prefixlen))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
999 subnets.append(current)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1000
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1001 return subnets
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1002
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1003 def supernet(self, prefixlen_diff=1):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1004 """The supernet containing the current network.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1005
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1006 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1007 prefixlen_diff: An integer, the amount the prefix length of the
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1008 network should be decreased by. For example, given a /96
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1009 network and a prefixlen_diff of 3, a supernet with a /93
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1010 netmask is returned.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1011
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1012 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1013 An IPv6 object.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1014
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1015 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1016 PrefixlenDiffInvalidError: If
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1017 self._prefixlen - prefixlen_diff < 0. I.e., you have a
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1018 negative prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1019
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1020 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1021 if self.prefixlen == 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1022 return self
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1023 if self.prefixlen - prefixlen_diff < 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1024 raise PrefixlenDiffInvalidError(
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1025 'current prefixlen is %d, cannot have a prefixlen_diff of %d' %
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1026 (self.prefixlen, prefixlen_diff))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1027 return IPv6(self.ip_ext + '/' + str(self.prefixlen - prefixlen_diff))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1028
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1029 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1030 def is_multicast(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1031 """Test if the address is reserved for multicast use.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1032
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1033 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1034 A boolean, True if the address is a multicast address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1035 See RFC 2373 2.7 for details.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1036
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1037 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1038 return self in _IPV6_RFC2373_MULTICAST
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1039
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1040 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1041 def is_unspecified(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1042 """Test if the address is unspecified.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1043
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1044 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1045 A boolean, True if this is the unspecified address as defined in
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1046 RFC 2373 2.5.2.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1047
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1048 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1049 return self == _IPV6_RFC2373_UNSPECIFIED
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1050
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1051 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1052 def is_loopback(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1053 """Test if the address is a loopback adddress.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1054
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1055 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1056 A boolean, True if the address is a loopback address as defined in
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1057 RFC 2373 2.5.3.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1058
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1059 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1060 return self == _IPV6_RFC2373_LOOPBACK
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1061
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1062 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1063 def is_link_local(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1064 """Test if the address is reserved for link-local.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1065
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1066 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1067 A boolean, True if the address is reserved per RFC 4291.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1068
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1069 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1070 return self in _IPV6_RFC4291_LINK_LOCAL
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1071
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1072 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1073 def is_site_local(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1074 """Test if the address is reserved for site-local.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1075
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1076 Note that the site-local address space has been deprecated by RFC 3879.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1077 Use is_private to test if this address is in the space of unique local
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1078 addresses as defined by RFC 4193.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1079
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1080 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1081 A boolean, True if the address is reserved per RFC 3513 2.5.6.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1082
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1083 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1084 return self in _IPV6_RFC3513_SITE_LOCAL
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1085
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1086 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1087 def is_private(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1088 """Test if this address is allocated for private networks.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1089
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1090 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1091 A boolean, True if the address is reserved per RFC 4193.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1092
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1093 """
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1094 return self in _IPV6_RFC4193_PRIVATE
53647
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1095
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1096 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1097 def version(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1098 return self._version
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1099
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1100 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1101 def packed(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1102 """The binary representation of this address."""
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1103 return struct.pack('!QQ', self.ip >> 64, self.ip & (2**64 - 1))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1104
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1105 def _is_shorthand_ip(self, ip_str=None):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1106 """Determine if the address is shortened.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1107
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1108 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1109 ip_str: A string, the IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1110
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1111 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1112 A boolean, True if the address is shortened.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1113
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1114 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1115 if ip_str.count('::') == 1:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1116 return True
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1117 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1118
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1119 def _explode_shorthand_ip_string(self, ip_str):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1120 """Expand a shortened IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1121
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1122 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1123 ip_str: A string, the IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1124
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1125 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1126 A string, the expanded IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1127
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1128 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1129 if self._is_shorthand_ip(ip_str):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1130 new_ip = []
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1131 hextet = ip_str.split('::')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1132 sep = len(hextet[0].split(':')) + len(hextet[1].split(':'))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1133 new_ip = hextet[0].split(':')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1134
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1135 for _ in xrange(8 - sep):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1136 new_ip.append('0000')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1137 new_ip += hextet[1].split(':')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1138
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1139 # Now need to make sure every hextet is 4 lower case characters.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1140 # If a hextet is < 4 characters, we've got missing leading 0's.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1141 ret_ip = []
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1142 for hextet in new_ip:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1143 ret_ip.append(('0' * (4 - len(hextet)) + hextet).lower())
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1144 return ':'.join(ret_ip)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1145 # We've already got a longhand ip_str.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1146 return ip_str
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1147
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1148 def _is_valid_ip(self, ip_str=None):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1149 """Ensure we have a valid IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1150
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1151 Probably not as exhaustive as it should be.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1152
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1153 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1154 ip_str: A string, the IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1155
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1156 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1157 A boolean, True if this is a valid IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1158
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1159 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1160 if not ip_str:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1161 ip_str = self.ip_ext
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1162
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1163 # We need to have at least one ':'.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1164 if ':' not in ip_str:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1165 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1166
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1167 # We can only have one '::' shortener.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1168 if ip_str.count('::') > 1:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1169 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1170
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1171 # '::' should be encompassed by start, digits or end.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1172 if ':::' in ip_str:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1173 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1174
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1175 # A single colon can neither start nor end an address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1176 if ((ip_str.startswith(':') and not ip_str.startswith('::')) or
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1177 (ip_str.endswith(':') and not ip_str.endswith('::'))):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1178 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1179
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1180 # If we have no concatenation, we need to have 8 fields with 7 ':'.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1181 if '::' not in ip_str and ip_str.count(':') != 7:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1182 # We might have an IPv4 mapped address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1183 if ip_str.count('.') != 3:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1184 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1185
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1186 ip_str = self._explode_shorthand_ip_string(ip_str)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1187
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1188 # Now that we have that all squared away, let's check that each of the
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1189 # hextets are between 0x0 and 0xFFFF.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1190 for hextet in ip_str.split(':'):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1191 if hextet.count('.') == 3:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1192 # If we have an IPv4 mapped address, the IPv4 portion has to be
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1193 # at the end of the IPv6 portion.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1194 if not ip_str.split(':')[-1] == hextet:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1195 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1196 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1197 IPv4(hextet)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1198 except IPv4IpValidationError:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1199 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1200 elif int(hextet, 16) < 0x0 or int(hextet, 16) > 0xFFFF:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1201 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1202 return True
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1203
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1204 def _is_valid_netmask(self, prefixlen):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1205 """Verify that the netmask/prefixlen is valid.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1206
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1207 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1208 prefixlen: A string, the netmask in prefix length format.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1209
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1210 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1211 A boolean, True if the prefix represents a valid IPv6
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1212 netmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1213
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1214 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1215 try:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1216 prefixlen = int(prefixlen)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1217 except ValueError:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1218 return False
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1219 return 0 <= prefixlen <= 128
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1220
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1221 def _ip_int_from_string(self, ip_str=None):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1222 """Turn an IPv6 address into an integer.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1223
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1224 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1225 ip_str: A string, the IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1226
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1227 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1228 A long, the IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1229
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1230 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1231 if not ip_str:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1232 ip_str = self.ip_ext
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1233
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1234 ip_int = 0
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1235
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1236 fields = self._explode_shorthand_ip_string(ip_str).split(':')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1237
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1238 # Do we have an IPv4 mapped (::ffff:a.b.c.d) or compact (::a.b.c.d)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1239 # address?
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1240 if fields[-1].count('.') == 3:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1241 ipv4_string = fields.pop()
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1242 ipv4_int = IPv4(ipv4_string).ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1243 octets = []
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1244 for _ in xrange(2):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1245 octets.append(hex(ipv4_int & 0xFFFF).lstrip('0x').rstrip('L'))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1246 ipv4_int >>= 16
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1247 fields.extend(reversed(octets))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1248
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1249 for field in fields:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1250 ip_int = (ip_int << 16) + int(field, 16)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1251
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1252 return ip_int
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1253
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1254 def _compress_hextets(self, hextets):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1255 """Compresses a list of hextets.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1256
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1257 Compresses a list of strings, replacing the longest continuous
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1258 sequence of "0" in the list with "" and adding empty strings at
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1259 the beginning or at the end of the string such that subsequently
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1260 calling ":".join(hextets) will produce the compressed version of
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1261 the IPv6 address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1262
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1263 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1264 hextets: A list of strings, the hextets to compress.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1265
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1266 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1267 A list of strings.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1268
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1269 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1270 best_doublecolon_start = -1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1271 best_doublecolon_len = 0
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1272 doublecolon_start = -1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1273 doublecolon_len = 0
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1274 for index in range(len(hextets)):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1275 if hextets[index] == '0':
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1276 doublecolon_len += 1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1277 if doublecolon_start == -1:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1278 # Start of a sequence of zeros.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1279 doublecolon_start = index
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1280 if doublecolon_len > best_doublecolon_len:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1281 # This is the longest sequence of zeros so far.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1282 best_doublecolon_len = doublecolon_len
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1283 best_doublecolon_start = doublecolon_start
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1284 else:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1285 doublecolon_len = 0
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1286 doublecolon_start = -1
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1287
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1288 if best_doublecolon_len > 1:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1289 best_doublecolon_end = (best_doublecolon_start +
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1290 best_doublecolon_len)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1291 # For zeros at the end of the address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1292 if best_doublecolon_end == len(hextets):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1293 hextets += ['']
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1294 hextets[best_doublecolon_start:best_doublecolon_end] = ['']
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1295 # For zeros at the beginning of the address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1296 if best_doublecolon_start == 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1297 hextets = [''] + hextets
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1298
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1299 return hextets
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1300
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1301 def _string_from_ip_int(self, ip_int=None):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1302 """Turns a 128-bit integer into hexadecimal notation.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1303
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1304 Args:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1305 ip_int: An integer, the IP address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1306
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1307 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1308 A string, the hexadecimal representation of the address.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1309
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1310 Raises:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1311 ValueError: The address is bigger than 128 bits of all ones.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1312
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1313 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1314 if not ip_int and ip_int != 0:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1315 ip_int = self.ip
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1316
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1317 if ip_int > self._ALL_ONES:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1318 raise ValueError('IPv6 address is too large')
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1319
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1320 hex_str = '%032x' % ip_int
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1321 hextets = []
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1322 for x in range(0, 32, 4):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1323 hextets.append('%x' % int(hex_str[x:x+4], 16))
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1324
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1325 hextets = self._compress_hextets(hextets)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1326 return ':'.join(hextets)
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1327
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1328 @property
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1329 def netmask_ext(self):
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1330 """IPv6 extended netmask.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1331
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1332 We don't deal with netmasks in IPv6 like we do in IPv4. This is
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1333 here strictly for IPv4 compatibility. We simply return the
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1334 prefix length.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1335
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1336 Returns:
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1337 An integer.
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1338
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1339 """
963f0f519709 Adds the ipaddr module to the standard library. Issue #3959.
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1340 return self.prefixlen
53703
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1341
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1342
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1343 # IPv4 constants.
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1344 _IPV4_RFC1918_NETWORKS = (IPv4('10.0.0.0/8'),
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1345 IPv4('172.16.0.0/12'),
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1346 IPv4('192.168.0.0/16'))
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1347 _IPV4_RFC3171_MULTICAST = IPv4('224.0.0.0/4')
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1348 _IPV4_RFC3330_LOOPBACK = IPv4('127.0.0.0/8')
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1349 _IPV4_RFC3927_LINK_LOCAL = IPv4('169.254.0.0/16')
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1350
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1351 # IPv6 constants.
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1352 _IPV6_RFC2373_MULTICAST = IPv6('ff00::/8')
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1353 _IPV6_RFC2373_UNSPECIFIED = IPv6('::')
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1354 _IPV6_RFC2373_LOOPBACK = IPv6('::1')
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1355 _IPV6_RFC4291_LINK_LOCAL = IPv6('fe80::/10')
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1356 _IPV6_RFC3513_SITE_LOCAL = IPv6('fec0::/10') # Deprecated by RFC3879.
ae24de48adc4 Optimization: move RFC defined network constant construction out of
Gregory P. Smith <greg@mad-scientist.com>
parents: 53672
diff changeset
1357 _IPV6_RFC4193_PRIVATE = IPv6('fc00::/7')