|
| 1 | +:mod:`ipaddress` --- IPv4/IPv6 manipulation library |
| 2 | +=================================================== |
| 3 | + |
| 4 | +.. module:: ipaddress |
| 5 | + :synopsis: IPv4/IPv6 manipulation library. |
| 6 | +.. moduleauthor:: Peter Moody |
| 7 | + |
| 8 | +**Source code:** :source:`Lib/ipaddress.py` |
| 9 | + |
| 10 | +-------------- |
| 11 | + |
| 12 | +The :mod:`ipaddress` module provides the capabilities to create, manipulate and |
| 13 | +operate on IPv4 and IPv6 addresses and networks. |
| 14 | + |
| 15 | +This is the full module API reference - for an overview and introduction, |
| 16 | +see :ref:`ipaddress-howto`. |
| 17 | + |
| 18 | +The functions and classes in this module make it straightforward to handle |
| 19 | +various tasks related to IP addresses, including checking whether or not two |
| 20 | +hosts are on the same subnet, iterating over all hosts in a particular |
| 21 | +subnet, as well as checking whether or not a string represents a valid |
| 22 | +IP address or network definition. |
| 23 | + |
| 24 | + |
| 25 | +Defining IP Addresses and Interfaces |
| 26 | +------------------------------------ |
| 27 | + |
| 28 | +The :mod:`ipaddress` module provides factory functions to define IP addresses |
| 29 | +and networks: |
| 30 | + |
| 31 | +.. function:: ip_address(address) |
| 32 | + |
| 33 | + Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on |
| 34 | + the IP address passed as argument. *address* is a string or integer |
| 35 | + representing the IP address. Either IPv4 or IPv6 addresses may be supplied; |
| 36 | + integers less than 2**32 will be considered to be IPv4 by default. A |
| 37 | + :exc:`ValueError` is raised if the *address* passed is neither an IPv4 nor |
| 38 | + IPv6 address. |
| 39 | + |
| 40 | + >>> ipaddress.ip_address('192.168.0.1') |
| 41 | + IPv4Address('192.168.0.1') |
| 42 | + >>> ipaddress.ip_address('2001:db8::') |
| 43 | + IPv6Address('2001:db8::') |
| 44 | + |
| 45 | + |
| 46 | +.. function:: ip_network(address, strict=True) |
| 47 | + |
| 48 | + Return an :class:`IPv4Network` or :class:`IPv6Network` object depending on |
| 49 | + the IP address passed as argument. *address* is a string or integer |
| 50 | + representing the IP network. Either IPv4 or IPv6 networks may be supplied; |
| 51 | + integers less than 2**32 will be considered to be IPv4 by default. *strict* |
| 52 | + is passed to :class:`IPv4Network` or :class:`IPv6Network` constructor. A |
| 53 | + :exc:`ValueError` is raised if the string passed isn't either an IPv4 or IPv6 |
| 54 | + address, or if the network has host bits set. |
| 55 | + |
| 56 | + >>> ipaddress.ip_network('192.168.0.0/28') |
| 57 | + IPv4Network('192.168.0.0/28') |
| 58 | + |
| 59 | + |
| 60 | +.. function:: ip_interface(address) |
| 61 | + |
| 62 | + Return an :class:`IPv4Interface` or :class:`IPv6Interface` object depending |
| 63 | + on the IP address passed as argument. *address* is a string or integer |
| 64 | + representing the IP address. Either IPv4 or IPv6 addresses may be supplied; |
| 65 | + integers less than 2**32 will be considered to be IPv4 by default.. A |
| 66 | + :exc:`ValueError` is raised if the *address* passed isn't either an IPv4 or |
| 67 | + IPv6 address. |
| 68 | + |
| 69 | + |
| 70 | +Representing IP Addresses and Networks |
| 71 | +-------------------------------------- |
| 72 | + |
| 73 | +The module defines the following and classes to represent IP addresses |
| 74 | +and networks: |
| 75 | + |
| 76 | +.. todo: list the properties and methods |
| 77 | +
|
| 78 | +.. class:: IPv4Address(address) |
| 79 | + |
| 80 | + Construct an IPv4 address. *address* is a string or integer representing the |
| 81 | + IP address. An :exc:`AddressValueError` is raised if *address* is not a |
| 82 | + valid IPv4 address. |
| 83 | + |
| 84 | + >>> ipaddress.IPv4Address('192.168.0.1') |
| 85 | + IPv4Address('192.168.0.1') |
| 86 | + >>> ipaddress.IPv4Address('192.0.2.1') == ipaddress.IPv4Address(3221225985) |
| 87 | + True |
| 88 | + |
| 89 | + |
| 90 | +.. class:: IPv4Interface(address) |
| 91 | + |
| 92 | + Construct an IPv4 interface. *address* is a string or integer representing |
| 93 | + the IP interface. An :exc:`AddressValueError` is raised if *address* is not |
| 94 | + a valid IPv4 address. |
| 95 | + |
| 96 | + The network address for the interface is determined by calling |
| 97 | + ``IPv4Network(address, strict=False)``. |
| 98 | + |
| 99 | + >>> ipaddress.IPv4Interface('192.168.0.0/24') |
| 100 | + IPv4Interface('192.168.0.0/24') |
| 101 | + >>> ipaddress.IPv4Interface('192.168.0.0/24').network |
| 102 | + IPv4Network('192.168.0.0/24') |
| 103 | + |
| 104 | + |
| 105 | +.. class:: IPv4Network(address, strict=True) |
| 106 | + |
| 107 | + Construct an IPv4 network. *address* is a string or integer representing the |
| 108 | + IP address (and optionally the network). An :exc:`AddressValueError` is |
| 109 | + raised if *address* is not a valid IPv4 address. A :exc:`NetmaskValueError` |
| 110 | + is raised if the netmask is not valid for an IPv4 address. |
| 111 | + |
| 112 | + If *strict* is ``True`` and host bits are set in the supplied address, |
| 113 | + then :exc:`ValueError` is raised. Otherwise, the host bits are masked out |
| 114 | + to determine the appropriate network address. |
| 115 | + |
| 116 | + >>> ipaddress.IPv4Network('192.0.2.0/27') |
| 117 | + IPv4Network('192.0.2.0/27') |
| 118 | + >>> ipaddress.IPv4Network('192.0.2.0/27').netmask |
| 119 | + IPv4Address('255.255.255.224') |
| 120 | + >>> ipaddress.IPv4Network('192.0.2.5/27', strict=False) |
| 121 | + IPv4Network('192.0.2.0/27') |
| 122 | + |
| 123 | + |
| 124 | +.. class:: IPv6Address(address) |
| 125 | + |
| 126 | + Construct an IPv6 address. *address* is a string or integer representing the |
| 127 | + IP address. An :exc:`AddressValueError` is raised if *address* is not a |
| 128 | + valid IPv6 address. |
| 129 | + |
| 130 | + >>> ipaddress.IPv6Address('2001:db8::1000') |
| 131 | + IPv6Address('2001:db8::1000') |
| 132 | + |
| 133 | + |
| 134 | +.. class:: IPv6Interface(address) |
| 135 | + |
| 136 | + Construct an IPv6 interface. *address* is a string or integer representing |
| 137 | + the IP interface. An :exc:`AddressValueError` is raised if *address* is not |
| 138 | + a valid IPv6 address. |
| 139 | + |
| 140 | + The network address for the interface is determined by calling |
| 141 | + ``IPv6Network(address, strict=False)``. |
| 142 | + |
| 143 | + >>> ipaddress.IPv6Interface('2001:db8::1000/96') |
| 144 | + IPv6Interface('2001:db8::1000/96') |
| 145 | + >>> ipaddress.IPv6Interface('2001:db8::1000/96').network |
| 146 | + IPv6Network('2001:db8::/96') |
| 147 | + |
| 148 | + |
| 149 | +.. class:: IPv6Network(address, strict=True) |
| 150 | + |
| 151 | + Construct an IPv6 network. *address* is a string or integer representing the |
| 152 | + IP address (and optionally the network). An :exc:`AddressValueError` is |
| 153 | + raised if *address* is not a valid IPv6 address. A :exc:`NetmaskValueError` |
| 154 | + is raised if the netmask is not valid for an IPv6 address. |
| 155 | + |
| 156 | + If *strict* is ``True`` and host bits are set in the supplied address, |
| 157 | + then :exc:`ValueError` is raised. Otherwise, the host bits are masked out |
| 158 | + to determine the appropriate network address. |
| 159 | + |
| 160 | + >>> ipaddress.IPv6Network('2001:db8::/96') |
| 161 | + IPv6Network('2001:db8::/96') |
| 162 | + >>> ipaddress.IPv6Network('2001:db8::/96').netmask |
| 163 | + IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::') |
| 164 | + >>> ipaddress.IPv6Network('2001:db8::1000/96', strict=False) |
| 165 | + IPv6Network('2001:db8::/96') |
| 166 | + |
| 167 | + |
| 168 | +Other Module Level Functions |
| 169 | +---------------------------- |
| 170 | + |
| 171 | +The module also provides the following module level functions: |
| 172 | + |
| 173 | +.. function:: v4_int_to_packed(address) |
| 174 | + |
| 175 | + Represent an address as 4 packed bytes in network (big-endian) order. |
| 176 | + *address* is an integer representation of an IPv4 IP address. A |
| 177 | + :exc:`ValueError` is raised if the integer is negative or too large to be an |
| 178 | + IPv4 IP address. |
| 179 | + |
| 180 | + >>> ipaddress.ip_address(3221225985) |
| 181 | + IPv4Address('192.0.2.1') |
| 182 | + >>> ipaddress.v4_int_to_packed(3221225985) |
| 183 | + b'\xc0\x00\x02\x01' |
| 184 | + |
| 185 | + |
| 186 | +.. function:: v6_int_to_packed(address) |
| 187 | + |
| 188 | + Represent an address as 16 packed bytes in network (big-endian) order. |
| 189 | + *address* is an integer representation of an IPv6 IP address. A |
| 190 | + :exc:`ValueError` is raised if the integer is negative or too large to be an |
| 191 | + IPv6 IP address. |
| 192 | + |
| 193 | + |
| 194 | +.. function:: summarize_address_range(first, last) |
| 195 | + |
| 196 | + Return an iterator of the summarized network range given the first and last |
| 197 | + IP addresses. *first* is the first :class:`IPv4Address` or |
| 198 | + :class:`IPv6Address` in the range and *last* is the last :class:`IPv4Address` |
| 199 | + or :class:`IPv6Address` in the range. A :exc:`TypeError` is raised if |
| 200 | + *first* or *last* are not IP addresses or are not of the same version. A |
| 201 | + :exc:`ValueError` is raised if *last* is not greater than *first* or if |
| 202 | + *first* address version is not 4 or 6. |
| 203 | + |
| 204 | + >>> [ipaddr for ipaddr in ipaddress.summarize_address_range( |
| 205 | + ... ipaddress.IPv4Address('192.0.2.0'), |
| 206 | + ... ipaddress.IPv4Address('192.0.2.130'))] |
| 207 | + [IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')] |
| 208 | + |
| 209 | + |
| 210 | +.. function:: collapse_addresses(addresses) |
| 211 | + |
| 212 | + Return an iterator of the collapsed :class:`IPv4Network` or |
| 213 | + :class:`IPv6Network` objects. *addresses* is an iterator of |
| 214 | + :class:`IPv4Network` or :class:`IPv6Network` objects. A :exc:`TypeError` is |
| 215 | + raised if *addresses* contains mixed version objects. |
| 216 | + |
| 217 | + >>> [ipaddr for ipaddr in |
| 218 | + ... ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'), |
| 219 | + ... ipaddress.IPv4Network('192.0.2.128/25')])] |
| 220 | + [IPv4Network('192.0.2.0/24')] |
| 221 | + |
| 222 | + |
| 223 | +.. function:: get_mixed_type_key(obj) |
| 224 | + |
| 225 | + Return a key suitable for sorting between networks and addresses. Address |
| 226 | + and Network objects are not sortable by default; they're fundamentally |
| 227 | + different, so the expression:: |
| 228 | + |
| 229 | + IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24') |
| 230 | + |
| 231 | + doesn't make sense. There are some times however, where you may wish to |
| 232 | + have :mod:`ipaddress` sort these anyway. If you need to do this, you can use |
| 233 | + this function as the ``key`` argument to :func:`sorted()`. |
| 234 | + |
| 235 | + *obj* is either a network or address object. |
| 236 | + |
| 237 | + |
| 238 | +Custom Exceptions |
| 239 | +----------------- |
| 240 | + |
| 241 | +To support more specific error reporting from class constructors, the |
| 242 | +module defines the following exceptions: |
| 243 | + |
| 244 | +.. exception:: AddressValueError(ValueError) |
| 245 | + |
| 246 | + Any value error related to the address. |
| 247 | + |
| 248 | + |
| 249 | +.. exception:: NetmaskValueError(ValueError) |
| 250 | + |
| 251 | + Any value error related to the netmask. |
0 commit comments