Registry / http-networking / netaddr

netaddr

JSON →
library1.3.0pypypi✓ verified 24d ago

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 netaddr
INSTALL
IMPORT
SIG · NETADDR
N
netaddr
http-networkingpythonv1.3.0
Install
1.7s avg
Import
100ms
Disk
26MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.104s · 27.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.7s · import 0.096s · 28MB
26MB installed
● package 26MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

IPAddress
from netaddr import IPAddress
IPNetwork
from netaddr import IPNetwork
IPSet
from netaddr import IPSet
EUI
from netaddr import EUI
mac_unix_common
from netaddr.formatters import mac_unix_common
Common formatter for MAC addresses

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.

from netaddr import IPNetwork, IPAddress, EUI # --- IP Network and Address Manipulation --- ip_net = IPNetwork('192.168.0.0/24') print(f"\nNetwork: {ip_net} (version {ip_net.version})") print(f"Number of hosts: {ip_net.num_hosts}") print(f"First usable address: {ip_net.first}") print(f"Last usable address: {ip_net.last}") # Iterate through hosts (excluding network and broadcast addresses) print("First 3 hosts in network:") for i, ip in enumerate(ip_net.hosts()): if i >= 3: break print(f" - {ip}") # Check if an IP is within a network ip_addr = IPAddress('192.168.0.10') print(f"Is {ip_addr} in {ip_net}? {ip_addr in ip_net}") # --- MAC Address (EUI) Manipulation --- mac = EUI('00-01-02-03-04-05') print(f"\nMAC Address: {mac}") print(f"MAC as integer: {int(mac)}") print(f"Vendor prefix (OUI): {mac.oui}") # Generate a different format print(f"MAC in Cisco format: {mac.format(dialect='cisco')}")
Debug
Known issues
breakingThe handling of RFC 6164 IPv6 point-to-point subnets changed. `netaddr` no longer reserves the first IP address, affecting `num_hosts` and address iteration for these specific subnets.
fix
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.
affects: < 0.9.0
breakingThe `is_loopback` property for `IPNetwork('::1/128')` (and similar IPv6 loopback representations) now correctly returns `True`. Prior versions returned `False`.
fix
Ensure your logic correctly identifies `::1/128` as a loopback address if it previously relied on the incorrect `False` return value.
affects: < 0.9.0
gotchaWhile core `IPAddress` and `IPNetwork` objects are generally treated as immutable once created, some operations on `IPSet` are in-place, and certain properties like `IPNetwork.netmask` can be reassigned. Be aware of mutable vs. immutable behavior.
fix
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.
affects: All versions
gotchaOperations often expect specific `netaddr` object types (`IPAddress`, `IPNetwork`, `EUI`). Passing raw strings or incompatible types can lead to errors. Always explicitly convert when necessary.
fix
Explicitly instantiate `IPAddress()`, `IPNetwork()`, or `EUI()` from strings or other data types before performing operations, rather than relying on implicit conversions.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'netaddr'
The 'netaddr' library is not installed in the Python environment where the code is being executed.
fix
pip install netaddr
ValueError: '10.0.0.150' is an Invalid IP version!
The 'IPAddress' constructor was provided with two arguments, where the second argument was mistakenly interpreted as the IP protocol version (expected as an integer like 4 or 6), instead of a separate IP address or a keyword argument for the version.
fix
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)
```
ValueError: invalid literal for int() with base 10: 'XXXX/111111//XY'
The string provided to 'IPNetwork' or 'IPAddress' is not a valid IP address or network in a format that 'netaddr' can parse, often containing non-numeric characters or an incorrect structure for a CIDR prefix.
fix
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')
```
AttributeError: 'IPNetwork' object has no attribute 'is_private'
The 'is_private' method was removed from 'IPAddress' and 'IPNetwork' objects in 'netaddr' version 1.0.0 as part of breaking changes, in favor of more precise methods.
fix
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.")
```
Upgrade
Version history
1.3.0latest on PyPI · released May 28, 2024
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
netaddr — pip install netaddr · libregistry