Python 3 standard library: ipaddress Internet address

1. ipaddress Internet address

The ipaddress module provides classes that handle IPv4 and IPv6 network addresses. These classes support authentication, finding addresses and hosts on the network, and other common operations.

1.1 address

The most basic object represents the network address itself. You can construct an address by passing in a string, integer, or byte sequence to ip_address(). The return value is an instance of IPV4Address or IPv6Address, depending on the type of address used.

import binascii
import ipaddress

ADDRESSES = [
    '10.9.0.6',
    'fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa',
]

for ip in ADDRESSES:
    addr = ipaddress.ip_address(ip)
    print('{!r}'.format(addr))
    print('   IP version:', addr.version)
    print('   is private:', addr.is_private)
    print('  packed form:', binascii.hexlify(addr.packed))
    print('      integer:', int(addr))
    print()

These two classes can provide different representations of the address to meet different purposes, and can also answer some basic assertions, such as whether the address is reserved for multicast communication, or whether it is in a private network.

1.2 network

The network is defined by an address range. It is usually represented by a base address and a mask, which indicates which part of the address represents the network and which part represents the address on the network. You can express the mask explicitly, or you can use a prefix length value, as shown in the following example.

import ipaddress

NETWORKS = [
    '10.9.0.0/24',
    'fdfd:87b5:b475:5e3e::/64',
]

for n in NETWORKS:
    net = ipaddress.ip_network(n)
    print('{!r}'.format(net))
    print('     is private:', net.is_private)
    print('      broadcast:', net.broadcast_address)
    print('     compressed:', net.compressed)
    print('   with netmask:', net.with_netmask)
    print('  with hostmask:', net.with_hostmask)
    print('  num addresses:', net.num_addresses)
    print()

As with addresses, there are two network classes corresponding to IPv4 and IPv6 networks. Each class provides properties or methods to access network related values, such as broadcast addresses and network addresses that hosts can use.

The network instance is iterative and provides the address on the network.  

import ipaddress

NETWORKS = [
    '10.9.0.0/24',
    'fdfd:87b5:b475:5e3e::/64',
]

for n in NETWORKS:
    net = ipaddress.ip_network(n)
    print('{!r}'.format(net))
    for i, ip in zip(range(3), net):
        print(ip)
    print()

This example only prints some addresses, because the IPv6 network may contain too many addresses to output all of them here.

The iterative processing network will provide addresses, but not all of them can be used as the legal addresses of the host. For example, the base address and broadcast address of the network are also included. To find addresses that can be used by regular hosts on the network, use the hosts() method, which generates a generator.

import ipaddress

NETWORKS = [
    '10.9.0.0/24',
    'fdfd:87b5:b475:5e3e::/64',
]

for n in NETWORKS:
    net = ipaddress.ip_network(n)
    print('{!r}'.format(net))
    for i, ip in zip(range(3), net.hosts()):
        print(ip)
    print()

Comparing the output of this example with that of the previous example, we can see that the host address does not contain the first few values generated when iterating the entire network.

In addition to the iterator protocol, the network also supports the in operator, which can be used to determine whether an address is part of a network.

import ipaddress

NETWORKS = [
    ipaddress.ip_network('10.9.0.0/24'),
    ipaddress.ip_network('fdfd:87b5:b475:5e3e::/64'),
]

ADDRESSES = [
    ipaddress.ip_address('10.9.0.6'),
    ipaddress.ip_address('10.7.0.31'),
    ipaddress.ip_address(
        'fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa'
    ),
    ipaddress.ip_address('fe80::3840:c439:b25e:63b0'),
]

for ip in ADDRESSES:
    for net in NETWORKS:
        if ip in net:
            print('{}\nis on {}'.format(ip, net))
            break
    else:
        print('{}\nis not on a known network'.format(ip))
    print()

The implementation of in uses a netmask to test the address, so it is much more efficient than expanding the complete address list on the network.

1.3 interface

The network interface represents a specific address on the network, which can be represented as a host address and a network prefix or network mask.

import ipaddress

ADDRESSES = [
    '10.9.0.6/24',
    'fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/64',
]

for ip in ADDRESSES:
    iface = ipaddress.ip_interface(ip)
    print('{!r}'.format(iface))
    print('network:\n  ', iface.network)
    print('ip:\n  ', iface.ip)
    print('IP with prefixlen:\n  ', iface.with_prefixlen)
    print('netmask:\n  ', iface.with_netmask)
    print('hostmask:\n  ', iface.with_hostmask)
    print()

The interface object contains some attributes, which can access the complete network and address respectively. In addition, it provides a variety of different methods to represent the interface and network mask.

Tags: Python network

Posted on Sun, 05 Apr 2020 20:07:15 -0700 by bluegreen1