netaddr is a Python library for manipulating network addresses, subnets, MAC addresses, and related network concepts. It supports IPv4, IPv6, EUI (MAC addresses), OUI, and IAB objects, providing a rich API for parsing, validating, converting, and performing operations like aggregation, intersection, and iteration. The current version is 1.3.0, and it maintains an active, albeit irregular, release cadence.
pip install netaddrVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the creation and basic manipulation of IPNetwork, IPAddress, and EUI objects. It shows how to inspect network properties, iterate through hosts, check for address membership, and format MAC addresses.
Review code that relied on the previous behavior of `num_hosts` or address iteration for RFC 6164 IPv6 subnets if migrating from versions older than 0.9.0.
Ensure your logic correctly identifies `::1/128` as a loopback address if it previously relied on the incorrect `False` return value.
For `IPSet` operations, be mindful of methods that modify the set in-place versus those that return a new set. For `IPAddress` and `IPNetwork`, generally assume immutability for their core network and address values.
Explicitly instantiate `IPAddress()`, `IPNetwork()`, or `EUI()` from strings or other data types before performing operations, rather than relying on implicit conversions.
pip install netaddr
Create separate 'IPAddress' objects for each IP or use the 'version' keyword argument explicitly.
```python
from netaddr import IPAddress
# Correct: two separate IPAddress objects
ip_start = IPAddress('10.0.0.150')
ip_end = IPAddress('10.0.0.1')
# Correct: specifying version explicitly
ipv4_address = IPAddress('192.168.1.1', version=4)
```Ensure the input string adheres to a valid IP address (e.g., '192.168.1.1') or CIDR network (e.g., '192.168.1.0/24') format. The 'IPAddress' class expects a single IP address, not a network with a mask or prefix in most cases.
```python
from netaddr import IPAddress, IPNetwork
# Correct: for a single IP address
ip = IPAddress('192.168.1.1')
# Correct: for an IP network in CIDR format
network = IPNetwork('192.168.1.0/24')
```Use the more specific replacement methods like 'is_link_local()', 'is_ipv4_private_use()', 'is_ipv6_unique_local()', or 'is_global()'. Alternatively, pin your 'netaddr' dependency to a version older than 1.0.0 (e.g., 'pip install netaddr==0.10.0') if you cannot update your code.
```python
from netaddr import IPAddress
ip = IPAddress('192.168.1.1')
# Instead of ip.is_private():
if ip.is_ipv4_private_use(): # For IPv4 private ranges (RFC 1918)
print(f"{ip} is an IPv4 private use address.")
elif ip.is_link_local():
print(f"{ip} is a link-local address.")
elif ip.is_ipv6_unique_local(): # For IPv6 unique local addresses
print(f"{ip} is an IPv6 unique local address.")
```No dependency data recorded yet.