This class represents and manipulates 32-bit IPv4 network + addresses.. Attributes: [examples for IPv4Network('192.0.2.0/27')] .network_address: IPv4Address('192.0.2.0') .hostmask: IPv4Address('0.0.0.31') .broadcast_address: IPv4Address('192.0.2.32') .netmask: IP
| 1482 | |
| 1483 | |
| 1484 | class IPv4Network(_BaseV4, _BaseNetwork): |
| 1485 | |
| 1486 | """This class represents and manipulates 32-bit IPv4 network + addresses.. |
| 1487 | |
| 1488 | Attributes: [examples for IPv4Network('192.0.2.0/27')] |
| 1489 | .network_address: IPv4Address('192.0.2.0') |
| 1490 | .hostmask: IPv4Address('0.0.0.31') |
| 1491 | .broadcast_address: IPv4Address('192.0.2.32') |
| 1492 | .netmask: IPv4Address('255.255.255.224') |
| 1493 | .prefixlen: 27 |
| 1494 | |
| 1495 | """ |
| 1496 | # Class to use when creating address objects |
| 1497 | _address_class = IPv4Address |
| 1498 | |
| 1499 | def __init__(self, address, strict=True): |
| 1500 | """Instantiate a new IPv4 network object. |
| 1501 | |
| 1502 | Args: |
| 1503 | address: A string or integer representing the IP [& network]. |
| 1504 | '192.0.2.0/24' |
| 1505 | '192.0.2.0/255.255.255.0' |
| 1506 | '192.0.2.0/0.0.0.255' |
| 1507 | are all functionally the same in IPv4. Similarly, |
| 1508 | '192.0.2.1' |
| 1509 | '192.0.2.1/255.255.255.255' |
| 1510 | '192.0.2.1/32' |
| 1511 | are also functionally equivalent. That is to say, failing to |
| 1512 | provide a subnetmask will create an object with a mask of /32. |
| 1513 | |
| 1514 | If the mask (portion after the / in the argument) is given in |
| 1515 | dotted quad form, it is treated as a netmask if it starts with a |
| 1516 | non-zero field (e.g. /255.0.0.0 == /8) and as a hostmask if it |
| 1517 | starts with a zero field (e.g. 0.255.255.255 == /8), with the |
| 1518 | single exception of an all-zero mask which is treated as a |
| 1519 | netmask == /0. If no mask is given, a default of /32 is used. |
| 1520 | |
| 1521 | Additionally, an integer can be passed, so |
| 1522 | IPv4Network('192.0.2.1') == IPv4Network(3221225985) |
| 1523 | or, more generally |
| 1524 | IPv4Interface(int(IPv4Interface('192.0.2.1'))) == |
| 1525 | IPv4Interface('192.0.2.1') |
| 1526 | |
| 1527 | Raises: |
| 1528 | AddressValueError: If ipaddress isn't a valid IPv4 address. |
| 1529 | NetmaskValueError: If the netmask isn't valid for |
| 1530 | an IPv4 address. |
| 1531 | ValueError: If strict is True and a network address is not |
| 1532 | supplied. |
| 1533 | """ |
| 1534 | addr, mask = self._split_addr_prefix(address) |
| 1535 | |
| 1536 | self.network_address = IPv4Address(addr) |
| 1537 | self.netmask, self._prefixlen = self._make_netmask(mask) |
| 1538 | packed = int(self.network_address) |
| 1539 | if packed & int(self.netmask) != packed: |
| 1540 | if strict: |
| 1541 | raise ValueError('%s has host bits set' % self) |
no outgoing calls
no test coverage detected
searching dependent graphs…