ipaddress
The Python ipaddress module provides tools for creating, manipulating, and working with IPv4 and IPv6 addresses and networks. It supports a wide range of standardized operations, including subnetting, address comparison, and validation:
>>> import ipaddress
>>> ip = ipaddress.ip_address("192.0.2.1")
>>> ip
IPv4Address('192.0.2.1')
>>> ip.is_private
True
The 192.0.2.0/24 subnet is reserved for documentation (TEST-NET-1), so .is_private is True because the address is not globally reachable.
Key Features
- Handles IPv4 and IPv6 addresses and networks
- Provides factory functions for creating address, network, and interface objects
- Supports comparisons and ordering for addresses, networks, and interfaces
- Enables membership tests to check whether an address belongs to a network
- Provides methods for subnetting, supernetting, and network relationship checks
- Supports iterating over networks and enumerating usable hosts
- Exposes address and network properties, such as private, multicast, and loopback
- Provides helpers for summarizing ranges and collapsing networks
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
ipaddress.ip_address() |
Function | Creates an IP address object from an address string or integer |
ipaddress.ip_network() |
Function | Creates an IP network object from an address/network string |
ipaddress.IPv4Address |
Class | Represents an IPv4 address |
ipaddress.IPv6Address |
Class | Represents an IPv6 address |
ipaddress.IPv4Network |
Class | Represents an IPv4 network |
ipaddress.IPv6Network |
Class | Represents an IPv6 network |
ipaddress.IPv4Address.is_private |
Property | Indicates whether the address is not globally reachable |
ipaddress.IPv6Address.is_private |
Property | Indicates whether the address is not globally reachable |
ipaddress.IPv4Network.hosts() |
Method | Returns an iterator over the usable hosts in the network |
ipaddress.IPv6Network.hosts() |
Method | Returns an iterator over the usable hosts in the network |
Examples
Checking if an IP address is private or not globally reachable:
>>> import ipaddress
>>> ip = ipaddress.ip_address("10.0.0.1")
>>> ip.is_private
True
Creating and iterating over hosts in an IP network:
>>> network = ipaddress.ip_network("192.0.2.0/29")
>>> list(network.hosts())
[
IPv4Address('192.0.2.1'),
IPv4Address('192.0.2.2'),
IPv4Address('192.0.2.3'),
IPv4Address('192.0.2.4'),
IPv4Address('192.0.2.5'),
IPv4Address('192.0.2.6')
]
Comparing IP addresses:
>>> ip1 = ipaddress.ip_address("192.0.2.1")
>>> ip2 = ipaddress.ip_address("192.0.2.2")
>>> ip1 < ip2
True
Common Use Cases
The most common tasks for ipaddress include:
- Validating and parsing IP addresses and networks from user input
- Checking whether an address belongs to a given network
- Calculating subnets and supernets for network planning
- Enumerating usable hosts in a subnet for provisioning or scanning
- Summarizing address ranges and collapsing networks for route aggregation
- Filtering addresses by properties, such as private, multicast, or loopback
- Representing host interfaces as address/prefix pairs
- Generating reverse DNS PTR names for addresses
Real-World Example
Suppose you need to filter a list of IP addresses to keep only those that belong to a specific subnet. You can achieve this by using the ipaddress module:
>>> import ipaddress
>>> addresses = ["192.0.2.1", "192.0.2.15", "198.51.100.2"]
>>> network = ipaddress.ip_network("192.0.2.0/24")
>>> [addr for addr in addresses if ipaddress.ip_address(addr) in network]
['192.0.2.1', '192.0.2.15']
In this example, you use the ipaddress module to filter out IP addresses that don’t belong to the specified network.
Related Resources
Tutorial
Learn IP Address Concepts With Python's ipaddress Module
In this step-by-step tutorial, you'll learn how to inspect and manipulate IP addresses as Python objects with Python's ipaddress module, improving your understanding of IP address mechanics and patterns used by the module.
By Leodanis Pozo Ramos • Updated June 6, 2026