The `ipaddress` module provides comprehensive tools for creating, manipulating, and operating on IPv4 and IPv6 addresses and networks. It is part of the Python 3 standard library (since Python 3.3) and available as a backport on PyPI for older Python versions. It simplifies common networking tasks such as IP address and network validation, subnetting, network relationship checks, and host enumeration.
pip install ipaddressVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse IP addresses and networks, check properties like version and privacy, and perform common network operations such as membership testing and subnetting using the `ipaddress` module.
For Python 3.3+, simply `import ipaddress`. For older Python versions, `pip install ipaddress` will provide the backport.
Migrate code to use the `ipaddress` module and its API. Avoid `ipaddr` entirely.
Ensure the host portion of network addresses is zeroed out for `Network` objects, or use `strict=False` if you understand the implications. For host addresses with a netmask, use `IPv4Interface` or `IPv6Interface`.
Explicitly cast strings to `unicode` for textual IP addresses in Python 2, and be mindful of `bytearray` for packed representations. For new development, prioritize Python 3 where `bytes` and `str` are distinct and well-defined.
Ensure the IP address or network string adheres to standard IPv4 (e.g., '192.168.1.1', '10.0.0.0/24') or IPv6 (e.g., '2001:db8::1', '2001:db8::/32') formatting.
Provide a network address where host bits are zero (e.g., `ipaddress.ip_network('192.168.1.0/24')`), or set `strict=False` to automatically mask out host bits (e.g., `ipaddress.ip_network('192.168.1.1/24', strict=False)`).Ensure that comparisons are only made between `ipaddress` objects of the same type and IP version. Convert objects to a common comparable format (e.g., integers) if version-agnostic comparison is needed, or explicitly check versions before comparing.
For Python 2.x, install the backport: `pip install ipaddress`. For Python 3.x, ensure your Python installation is complete and the module is accessible, or check for virtual environment issues.
No dependency data recorded yet.