ipaddress - IPv4/IPv6 manipulation library in Python

PythonServer Side ProgrammingProgramming

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 as "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.

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
>>> ipaddress.ip_address('192.168.0.1')
IPv4Address('192.168.0.1')
>>> ipaddress.ip_address('2001:ab7::')
IPv6Address('2001:ab7::')

ip_network()

Return an IPv4Network or IPv6Network object depending on the IP address passed as argument.

>>> ipaddress.ip_network('192.168.100.0/24')
IPv4Network('192.168.100.0/24')
>>> ipaddress.ip_network('2001:db8:abcd:100::/64')
IPv6Network('2001:db8:abcd:100::/64')

ip_interface()

Return an IPv4Interface or IPv6Interface object depending on the IP address passed as argument.

>>> ipaddress.ip_interface('192.168.100.10/24')
IPv4Interface('192.168.100.10/24')
>>> ipaddress.ip_interface('2001:db8:abcd:100::1/64')
IPv6Interface('2001:db8:abcd:100::1/64')

The ipaddress module defines following classes:

IPv4Address(address)

This constructor returns an IPv4 address object.

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).

>>> ipaddress.IPv4Address('192.168.0.1')
IPv4Address('192.168.0.1')
>>> ipaddress.IPv4Address(3162581505)
IPv4Address('188.129.42.1')
>>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
IPv4Address('192.168.0.1')

IPv6Address()

Construct an IPv6 address.

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.

>>> ipaddress.IPv6Address('2001:db8::1000')
IPv6Address('2001:db8::1000')
>>> ipaddress.IPv6Address("::abc:7:def")
IPv6Address('::abc:7:def')

Version

Returns the appropriate version number

>>> add = ipaddress.IPv4Address('192.168.0.1')
>>> add.version
4
>>> ip = ipaddress.IPv6Address('2001:db8::1000')
>>> ip.version
6
raja
Published on 16-Apr-2019 12:15:32
Advertisements