| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python3 | 1 #!/usr/bin/python3 |
| 2 # | 2 # |
| 3 # Copyright 2007 Google Inc. | 3 # Copyright 2007 Google Inc. |
| 4 # Licensed to PSF under a Contributor Agreement. | 4 # Licensed to PSF under a Contributor Agreement. |
| 5 # | 5 # |
| 6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 # you may not use this file except in compliance with the License. | 7 # you may not use this file except in compliance with the License. |
| 8 # You may obtain a copy of the License at | 8 # You may obtain a copy of the License at |
| 9 # | 9 # |
| 10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 """A Value Error related to the netmask.""" | 38 """A Value Error related to the netmask.""" |
| 39 | 39 |
| 40 | 40 |
| 41 def ip_address(address, version=None): | 41 def ip_address(address, version=None): |
| 42 """Take an IP string/int and return an object of the correct type. | 42 """Take an IP string/int and return an object of the correct type. |
| 43 | 43 |
| 44 Args: | 44 Args: |
| 45 address: A string or integer, the IP address. Either IPv4 or | 45 address: A string or integer, the IP address. Either IPv4 or |
| 46 IPv6 addresses may be supplied; integers less than 2**32 will | 46 IPv6 addresses may be supplied; integers less than 2**32 will |
| 47 be considered to be IPv4 by default. | 47 be considered to be IPv4 by default. |
| 48 version: An Integer, 4 or 6. If set, don't try to automatically | 48 version: An integer, 4 or 6. If set, don't try to automatically |
| 49 determine what the IP address type is. important for things | 49 determine what the IP address type is. Important for things |
| 50 like ip_address(1), which could be IPv4, '192.0.2.1', or IPv6, | 50 like ip_address(1), which could be IPv4, '192.0.2.1', or IPv6, |
| 51 '2001:db8::1'. | 51 '2001:db8::1'. |
| 52 | 52 |
| 53 Returns: | 53 Returns: |
| 54 An IPv4Address or IPv6Address object. | 54 An IPv4Address or IPv6Address object. |
| 55 | 55 |
| 56 Raises: | 56 Raises: |
| 57 ValueError: if the string passed isn't either a v4 or a v6 | 57 ValueError: if the *address* passed isn't either a v4 or a v6 |
| 58 address. | 58 address, or if the version is not None, 4, or 6. |
| 59 | 59 |
| 60 """ | 60 """ |
| 61 if version: | 61 if version is not None: |
| 62 if version == 4: | 62 if version == 4: |
| 63 return IPv4Address(address) | 63 return IPv4Address(address) |
| 64 elif version == 6: | 64 elif version == 6: |
| 65 return IPv6Address(address) | 65 return IPv6Address(address) |
| 66 else: | |
| 67 raise ValueError() | |
|
ezio.melotti
2012/05/23 01:37:15
Shouldn't this have a message?
| |
| 66 | 68 |
| 67 try: | 69 try: |
| 68 return IPv4Address(address) | 70 return IPv4Address(address) |
| 69 except (AddressValueError, NetmaskValueError): | 71 except (AddressValueError, NetmaskValueError): |
| 70 pass | 72 pass |
| 71 | 73 |
| 72 try: | 74 try: |
| 73 return IPv6Address(address) | 75 return IPv6Address(address) |
| 74 except (AddressValueError, NetmaskValueError): | 76 except (AddressValueError, NetmaskValueError): |
| 75 pass | 77 pass |
| 76 | 78 |
| 77 raise ValueError('%r does not appear to be an IPv4 or IPv6 address' % | 79 raise ValueError('%r does not appear to be an IPv4 or IPv6 address' % |
| 78 address) | 80 address) |
| 79 | 81 |
| 80 | 82 |
| 81 def ip_network(address, version=None, strict=True): | 83 def ip_network(address, version=None, strict=True): |
| 82 """Take an IP string/int and return an object of the correct type. | 84 """Take an IP string/int and return an object of the correct type. |
| 83 | 85 |
| 84 Args: | 86 Args: |
| 85 address: A string or integer, the IP network. Either IPv4 or | 87 address: A string or integer, the IP network. Either IPv4 or |
| 86 IPv6 networks may be supplied; integers less than 2**32 will | 88 IPv6 networks may be supplied; integers less than 2**32 will |
| 87 be considered to be IPv4 by default. | 89 be considered to be IPv4 by default. |
| 88 version: An Integer, if set, don't try to automatically | 90 version: An integer, 4 or 6. If set, don't try to automatically |
|
ezio.melotti
2012/05/23 01:37:15
Ditto for the comma.
| |
| 89 determine what the IP address type is. important for things | 91 determine what the IP address type is. Important for things |
| 90 like ip_network(1), which could be IPv4, '192.0.2.1/32', or IPv6, | 92 like ip_network(1), which could be IPv4, '192.0.2.1/32', or IPv6, |
| 91 '2001:db8::1/128'. | 93 '2001:db8::1/128'. |
|
ezio.melotti
2012/05/23 01:37:15
The strict arg is not documented.
| |
| 92 | 94 |
| 93 Returns: | 95 Returns: |
| 94 An IPv4Network or IPv6Network object. | 96 An IPv4Network or IPv6Network object. |
| 95 | 97 |
| 96 Raises: | 98 Raises: |
| 97 ValueError: if the string passed isn't either a v4 or a v6 | 99 ValueError: if the string passed isn't either a v4 or a v6 |
| 98 address. Or if the network has host bits set. | 100 address. Or if the network has host bits set. Or if the version |
| 101 is not None, 4, or 6. | |
| 99 | 102 |
| 100 """ | 103 """ |
| 101 if version: | 104 if version is not None: |
| 102 if version == 4: | 105 if version == 4: |
| 103 return IPv4Network(address, strict) | 106 return IPv4Network(address, strict) |
| 104 elif version == 6: | 107 elif version == 6: |
| 105 return IPv6Network(address, strict) | 108 return IPv6Network(address, strict) |
| 109 else: | |
| 110 raise ValueError() | |
| 106 | 111 |
| 107 try: | 112 try: |
| 108 return IPv4Network(address, strict) | 113 return IPv4Network(address, strict) |
| 109 except (AddressValueError, NetmaskValueError): | 114 except (AddressValueError, NetmaskValueError): |
| 110 pass | 115 pass |
| 111 | 116 |
| 112 try: | 117 try: |
| 113 return IPv6Network(address, strict) | 118 return IPv6Network(address, strict) |
| 114 except (AddressValueError, NetmaskValueError): | 119 except (AddressValueError, NetmaskValueError): |
| 115 pass | 120 pass |
| 116 | 121 |
| 117 raise ValueError('%r does not appear to be an IPv4 or IPv6 network' % | 122 raise ValueError('%r does not appear to be an IPv4 or IPv6 network' % |
| 118 address) | 123 address) |
| 119 | 124 |
| 120 | 125 |
| 121 def ip_interface(address, version=None): | 126 def ip_interface(address, version=None): |
| 122 """Take an IP string/int and return an object of the correct type. | 127 """Take an IP string/int and return an object of the correct type. |
| 123 | 128 |
| 124 Args: | 129 Args: |
| 125 address: A string or integer, the IP address. Either IPv4 or | 130 address: A string or integer, the IP address. Either IPv4 or |
| 126 IPv6 addresses may be supplied; integers less than 2**32 will | 131 IPv6 addresses may be supplied; integers less than 2**32 will |
| 127 be considered to be IPv4 by default. | 132 be considered to be IPv4 by default. |
| 128 version: An Integer, if set, don't try to automatically | 133 version: An integer, 4 or 6. If set, don't try to automatically |
| 129 determine what the IP address type is. important for things | 134 determine what the IP address type is. Important for things |
| 130 like ip_network(1), which could be IPv4, '192.0.2.1/32', or IPv6, | 135 like ip_interface(1), which could be IPv4, '192.0.2.1/32', or IPv6, |
| 131 '2001:db8::1/128'. | 136 '2001:db8::1/128'. |
| 132 | 137 |
| 133 Returns: | 138 Returns: |
| 134 An IPv4Network or IPv6Network object. | 139 An IPv4Interface or IPv6Interface object. |
| 135 | 140 |
| 136 Raises: | 141 Raises: |
| 137 ValueError: if the string passed isn't either a v4 or a v6 | 142 ValueError: if the string passed isn't either a v4 or a v6 |
| 138 address. | 143 address. Or if the version is not None, 4, or 6. |
| 139 | 144 |
| 140 Notes: | 145 Notes: |
| 141 The IPv?Interface classes describe an Address on a particular | 146 The IPv?Interface classes describe an Address on a particular |
| 142 Network, so they're basically a combination of both the Address | 147 Network, so they're basically a combination of both the Address |
| 143 and Network classes. | 148 and Network classes. |
| 144 """ | 149 """ |
| 145 if version: | 150 if version is not None: |
| 146 if version == 4: | 151 if version == 4: |
| 147 return IPv4Interface(address) | 152 return IPv4Interface(address) |
| 148 elif version == 6: | 153 elif version == 6: |
| 149 return IPv6Interface(address) | 154 return IPv6Interface(address) |
| 155 else: | |
| 156 raise ValueError() | |
| 150 | 157 |
| 151 try: | 158 try: |
| 152 return IPv4Interface(address) | 159 return IPv4Interface(address) |
| 153 except (AddressValueError, NetmaskValueError): | 160 except (AddressValueError, NetmaskValueError): |
| 154 pass | 161 pass |
| 155 | 162 |
| 156 try: | 163 try: |
| 157 return IPv6Interface(address) | 164 return IPv6Interface(address) |
| 158 except (AddressValueError, NetmaskValueError): | 165 except (AddressValueError, NetmaskValueError): |
| 159 pass | 166 pass |
| 160 | 167 |
| 161 raise ValueError('%r does not appear to be an IPv4 or IPv6 network' % | 168 raise ValueError('%r does not appear to be an IPv4 or IPv6 network' % |
| 162 address) | 169 address) |
| 163 | 170 |
| 164 | 171 |
| 165 def v4_int_to_packed(address): | 172 def v4_int_to_packed(address): |
| 166 """The binary representation of this address. | 173 """Represent an address as 4 packed bytes in network (big-endian) order. |
| 167 | 174 |
| 168 Args: | 175 Args: |
| 169 address: An integer representation of an IPv4 IP address. | 176 address: An integer representation of an IPv4 IP address. |
| 170 | 177 |
| 171 Returns: | 178 Returns: |
| 172 The binary representation of this address. | 179 The integer address packed as 4 bytes in network (big-endian) order. |
| 173 | 180 |
| 174 Raises: | 181 Raises: |
| 175 ValueError: If the integer is too large to be an IPv4 IP | 182 ValueError: If the integer is is negative or too large to be an |
| 176 address. | 183 IPv4 IP address. |
| 177 """ | 184 """ |
| 178 if address > _BaseV4._ALL_ONES: | 185 try: |
| 179 raise ValueError('Address too large for IPv4') | 186 return struct.pack('!I', address) |
| 180 return struct.pack('!I', address) | 187 except: |
| 188 raise ValueError("Address negative or too large for IPv4") | |
|
ezio.melotti
2012/05/23 01:37:15
I don't like the bare except too much. Isn't the
| |
| 181 | 189 |
| 182 | 190 |
| 183 def v6_int_to_packed(address): | 191 def v6_int_to_packed(address): |
| 184 """The binary representation of this address. | 192 """Represent an address as 16 packed bytes in network (big-endian) order. |
| 185 | 193 |
| 186 Args: | 194 Args: |
| 187 address: An integer representation of an IPv4 IP address. | 195 address: An integer representation of an IPv4 IP address. |
| 188 | 196 |
| 189 Returns: | 197 Returns: |
| 190 The binary representation of this address. | 198 The integer address packed as 16 bytes in network (big-endian) order. |
| 191 """ | 199 """ |
| 192 return struct.pack('!QQ', address >> 64, address & (2**64 - 1)) | 200 try: |
| 201 return struct.pack('!QQ', address >> 64, address & (2**64 - 1)) | |
| 202 except: | |
| 203 raise ValueError("Address negative or too large for IPv6") | |
| 193 | 204 |
| 194 | 205 |
| 195 def _find_address_range(addresses): | 206 def _find_address_range(addresses): |
| 196 """Find a sequence of addresses. | 207 """Find a sequence of IPv#Address. |
| 197 | 208 |
| 198 Args: | 209 Args: |
| 199 addresses: a list of IPv4 or IPv6 addresses. | 210 addresses: a list of IPv#Address objects. |
|
ezio.melotti
2012/05/23 01:37:15
I prefer "IPv4Address or IPv6Address"
| |
| 200 | 211 |
| 201 Returns: | 212 Returns: |
| 202 A tuple containing the first and last IP addresses in the sequence. | 213 A tuple containing the first and last IP addresses in the sequence. |
| 203 | 214 |
| 204 """ | 215 """ |
| 205 first = last = addresses[0] | 216 first = last = addresses[0] |
| 206 for ip in addresses[1:]: | 217 for ip in addresses[1:]: |
| 207 if ip._ip == last._ip + 1: | 218 if ip._ip == last._ip + 1: |
| 208 last = ip | 219 last = ip |
| 209 else: | 220 else: |
| (...skipping 1974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2184 def with_prefixlen(self): | 2195 def with_prefixlen(self): |
| 2185 return '%s/%d' % (str(self.network_address), self._prefixlen) | 2196 return '%s/%d' % (str(self.network_address), self._prefixlen) |
| 2186 | 2197 |
| 2187 @property | 2198 @property |
| 2188 def with_netmask(self): | 2199 def with_netmask(self): |
| 2189 return '%s/%s' % (str(self.network_address), str(self.netmask)) | 2200 return '%s/%s' % (str(self.network_address), str(self.netmask)) |
| 2190 | 2201 |
| 2191 @property | 2202 @property |
| 2192 def with_hostmask(self): | 2203 def with_hostmask(self): |
| 2193 return '%s/%s' % (str(self.network_address), str(self.hostmask)) | 2204 return '%s/%s' % (str(self.network_address), str(self.hostmask)) |
| OLD | NEW |