Google's IP Address Manipulation Library
ipaddr (pypi-slug: ipaddr) is a Python library developed by Google for manipulating IPv4 and IPv6 addresses and networks. It provides functionalities for validation, subnet operations, and summarization. The last release was version 2.2.0 in 2017. This library has been superseded by the `ipaddress` module, which is part of the Python 3 standard library.
Warnings
- breaking The `ipaddr` library has been officially superseded by the `ipaddress` module, which is included in the Python 3 standard library. The PyPI package for `ipaddr` is primarily a backport for Python 2. Users on Python 3 should migrate to `ipaddress` for future compatibility and maintenance.
- gotcha When creating `IPNetwork` objects with `ipaddr`, the library is more lenient about host bits being set compared to the `ipaddress` module. In `ipaddress`, creating a network object like `ipaddress.ip_network('192.168.1.5/24')` would raise a `ValueError` because the host bits are set. `ipaddr` might implicitly correct this or allow it depending on the version and specific method.
- deprecated The `ipaddr.IPAddress()` and `ipaddr.IPNetwork()` factory functions had equivalent, but slightly different, behavior and naming conventions compared to the `ipaddress.ip_address()` and `ipaddress.ip_network()` factory functions in the standard library.
Install
-
pip install ipaddr
Imports
- IPAddress
from ipaddr import IPAddress
- IPNetwork
from ipaddr import IPNetwork
Quickstart
import ipaddr
# Create an IPv4 address object
ip_v4 = ipaddr.IPAddress('192.168.1.1')
print(f"IPv4 Address: {ip_v4}")
print(f"Is private: {ip_v4.is_private}")
# Create an IPv6 address object
ip_v6 = ipaddr.IPAddress('2001:db8::1')
print(f"IPv6 Address: {ip_v6}")
# Create a network object
network = ipaddr.IPNetwork('192.168.1.0/24')
print(f"Network: {network}")
print(f"Network address: {network.network}")
print(f"Broadcast address: {network.broadcast}")
print(f"Number of hosts: {network.numhosts}")
# Check if an address is in a network
print(f"192.168.1.5 in network: {ipaddr.IPAddress('192.168.1.5') in network}")