Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
ipaddress - IPv4/IPv6 manipulation library in Python
Internet Protocol is currently in the process of moving from version 4 to version 6. This is necessitated because version 4 doesn't provide enough addresses to handle the increasing number of devices with direct connections to the internet.
An IPv4 address is composed of 32 bits, represented into four eight-bit groups called "octets". This is a "dotted decimal" format where each eight-bit octet can have a decimal value 0 to 255.
For example: 192.168.1.1
IPv4 address with CIDR notation: 192.168.1.1/24 where 24 means first three octets identify the network and last octet identifies node.
An IPv6 address is 128 bits long. It uses hexadecimal notation. Each position in an IPv6 address represents four bits with a value from 0 to f. The 128 bits are divided into 8 groupings of 16 bits each separated by colons.
Example: 2001:db8:abcd:100::1/64
All IPv6 addresses use CIDR notation to determine how many of the leading bits are used for network identification and rest for host/interface identification.
Python's standard library has ipaddress module which provides the capabilities to create, manipulate and operate on IPv4 and IPv6 addresses and networks.
Factory Functions
The module provides following factory functions to conveniently create IP addresses, networks and interfaces ?
ip_address()
Return an IPv4Address or IPv6Address object depending on the IP address passed as argument. Either IPv4 or IPv6 addresses may be supplied ?
import ipaddress
# Create IPv4 address
ipv4 = ipaddress.ip_address('192.168.0.1')
print(ipv4)
# Create IPv6 address
ipv6 = ipaddress.ip_address('2001:ab7::')
print(ipv6)
192.168.0.1 2001:ab7::
ip_network()
Return an IPv4Network or IPv6Network object depending on the IP address passed as argument ?
import ipaddress
# Create IPv4 network
net4 = ipaddress.ip_network('192.168.100.0/24')
print(net4)
# Create IPv6 network
net6 = ipaddress.ip_network('2001:db8:abcd:100::/64')
print(net6)
192.168.100.0/24 2001:db8:abcd:100::/64
ip_interface()
Return an IPv4Interface or IPv6Interface object depending on the IP address passed as argument ?
import ipaddress
# Create IPv4 interface
int4 = ipaddress.ip_interface('192.168.100.10/24')
print(int4)
# Create IPv6 interface
int6 = ipaddress.ip_interface('2001:db8:abcd:100::1/64')
print(int6)
192.168.100.10/24 2001:db8:abcd:100::1/64
IPv4Address Class
This constructor returns an IPv4 address object. A valid IPv4 address is identified by following ?
- A string in decimal-dot notation, consisting of four decimal integers in the inclusive range 0-255, separated by dots (e.g. 192.168.0.1).
- An integer whose binary equivalent is 32 bits.
- An integer packed into a bytes object of length 4 (most significant octet first).
import ipaddress
# String format
addr1 = ipaddress.IPv4Address('192.168.0.1')
print(addr1)
# Integer format
addr2 = ipaddress.IPv4Address(3162581505)
print(addr2)
# Bytes format
addr3 = ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
print(addr3)
192.168.0.1 188.129.42.1 192.168.0.1
IPv6Address Class
Construct an IPv6 address. A valid IPv6 address is constituted as follows ?
- A string consisting of eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons.
- An integer that fits into 128 bits.
- An integer packed into a bytes object of length 16, big-endian.
import ipaddress
# Full format
addr1 = ipaddress.IPv6Address('2001:db8::1000')
print(addr1)
# Compressed format
addr2 = ipaddress.IPv6Address("::abc:7:def")
print(addr2)
2001:db8::1000 ::abc:7:def
Version Property
Returns the appropriate version number ?
import ipaddress
# IPv4 version
addr4 = ipaddress.IPv4Address('192.168.0.1')
print(f"IPv4 version: {addr4.version}")
# IPv6 version
addr6 = ipaddress.IPv6Address('2001:db8::1000')
print(f"IPv6 version: {addr6.version}")
IPv4 version: 4 IPv6 version: 6
Conclusion
Python's ipaddress module provides convenient factory functions and classes for working with IP addresses and networks. Use ip_address() for creating address objects and the specific classes for advanced manipulation and validation.
