Last modified: Sep 13, 2026
Python IP Address Library Guide
Working with IP addresses in Python can be tricky without the right tools. The ipaddress module simplifies this task. It allows developers to create, manipulate, and validate both IPv4 and IPv6 addresses.
This article will walk you through the basics of using the ipaddress library in Python. We'll cover common use cases, functions, and practical examples to help you get started quickly.
What Is the ipaddress Module?
The ipaddress module was introduced in Python 3.3. It provides classes to represent IP addresses and networks. These classes include:
IPv4AddressIPv6AddressIPv4NetworkIPv6Network
You don’t need to install anything extra. Just import it at the top of your script:
import ipaddress
Creating an IP Address Object
To work with an IP address, you must first create an object. Use the ip_address() function for flexibility:
import ipaddress
# Create IPv4 address
ipv4 = ipaddress.ip_address("192.168.1.1")
print(ipv4)
# Create IPv6 address
ipv6 = ipaddress.ip_address("2001:db8::1")
print(ipv6)
192.168.1.1
2001:db8::1
The ip_address() function automatically detects whether the input is IPv4 or IPv6.
Validating IP Addresses
One major benefit of the ipaddress module is easy validation. If you pass an invalid string, Python raises a ValueError:
import ipaddress
try:
# Invalid IP address
ipaddress.ip_address("999.999.999.999")
except ValueError as e:
print("Invalid IP:", e)
Invalid IP: '999.999.999.999' does not appear to be an IPv4 or IPv6 address
This behavior helps prevent bugs caused by malformed data.
Checking IP Version
Each IP address object has a version attribute. It tells you if it’s IPv4 or IPv6:
import ipaddress
ipv4 = ipaddress.ip_address("192.168.1.1")
ipv6 = ipaddress.ip_address("2001:db8::1")
print("IPv4 version:", ipv4.version)
print("IPv6 version:", ipv6.version)
IPv4 version: 4
IPv6 version: 6
This is useful when your application needs to handle both versions differently.
Working With IP Networks
Beyond individual addresses, the ipaddress module supports network operations. You can create network objects using ip_network():
import ipaddress
# Create a network
network = ipaddress.ip_network("192.168.1.0/24")
print(network)
192.168.1.0/24
The /24 defines the subnet mask. The module handles CIDR notation automatically.
Listing All Hosts in a Network
Need to list all usable hosts in a subnet? Use the hosts() method:
import ipaddress
network = ipaddress.ip_network("192.168.1.0/30")
for host in network.hosts():
print(host)
192.168.1.1
192.168.1.2
A /30 network has four addresses. Two are reserved. Only two are usable.
Checking if an IP Belongs to a Network
Use the in operator to check membership:
import ipaddress
network = ipaddress.ip_network("192.168.1.0/24")
ip = ipaddress.ip_address("192.168.1.50")
if ip in network:
print("IP is part of the network.")
IP is part of the network.
This feature is essential for access control or routing logic.
Getting Network Details
The ipaddress module exposes several helpful properties:
import ipaddress
network = ipaddress.ip_network("192.168.1.0/24")
print("Network:", network.network_address)
print("Broadcast:", network.broadcast_address)
print("Netmask:", network.netmask)
print("Prefix Length:", network.prefixlen)
Network: 192.168.1.0
Broadcast: 192.168.1.255
Netmask: 255.255.255.0
Prefix Length: 24
These values are useful for configuration tools or diagnostic scripts.
Converting Between IP and Integer
Sometimes you need to convert an IP address to an integer. The ipaddress module makes this easy:
import ipaddress
ipv4 = ipaddress.ip_address("192.168.1.1")
ipv6 = ipaddress.ip_address("2001:db8::1")
print("IPv4 as int:", int(ipv4))
print("IPv6 as int:", int(ipv6))
IPv4 as int: 3232235777
IPv6 as int: 42540488161975842760550356425300246529
You can also convert back using int() and ip_address().
Iterating Over a Network Range
To loop through every address in a network, iterate directly:
import ipaddress
network = ipaddress.ip_network("192.168.1.0/30")
for ip in network:
print(ip)
192.168.1.0
192.168.1.1
192.168.1.2
192.168.1.3
Note that this includes network and broadcast addresses.
Handling Strict Network Definitions
By default, ip_network() applies host bits. To enforce strict mode, set strict=True:
import ipaddress
# This raises an error in strict mode
try:
network = ipaddress.ip_network("192.168.1.1/24", strict=True)
except ValueError as e:
print("Error:", e)
Error: 192.168.1.1/24 has host bits set
Strict mode catches configuration mistakes early.
Summing Up
The ipaddress module is a powerful built-in tool for handling IP addresses in Python. Whether validating input, analyzing networks, or converting formats, it covers most needs out of the box.
Start using it today to write cleaner and more reliable network-related code. With just a few lines, you can avoid manual parsing and reduce bugs significantly.