Column address: a Python module per week

Meanwhile, welcome to follow my wechat official account AlwaysBeta, more exciting content waiting for you.

The ipAddress module includes classes for handling IPv4 and IPv6 network addresses. These classes support validation, locating addresses and hosts on the network, and other common operations.

address

The most basic object represents the network address itself. Pass a string, integer, or byte sequence to ip_address() to construct the address. The return value is either IPv4Address or an instance of IPv6Address, depending on the address type 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()
    
# output
# IPv4Address (' 10.9.0.6)
# IP version: 4
# is private: True
# packed form: b'0a090006'
# integer: 168361990
# 
# IPv6Address('fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa')
# IP version: 6
# is private: True
# packed form: b'fdfd87b5b4755e3eb1bce121a8eb14aa'
# integer: 337611086560236126439725644408160982186
Copy the code

You can also use this method to verify that the IP address is valid:

import ipaddress


def  is_ip_Valid(ipaddr):
    try:
        ipaddress.ip_address(ipaddr);
        return True;
    except :
        return False;
     
if __name__ == '__main__':
    print(is_ip_Valid('2001:db8::'));
    print(is_ip_Valid('192.168.168.1'));
Copy the code

network

A network consists of a series of addresses, usually in the form of addresses and masks.

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()
    
# output
# IPv4Network (' 10.9.0.0/24)
# is private: True
# broadcast: 10.9.0.255
# compressed: 10.9.0.0/24
# with netmask: 10.9.0.0/255.255.255.0
# with hostmask: 10.9.0.0/0.0.0.255
# num addresses: 256
# 
# IPv6Network('fdfd:87b5:b475:5e3e::/64')
# is private: True
# broadcast: fdfd:87b5:b475:5e3e:ffff:ffff:ffff:ffff
# compressed: fdfd:87b5:b475:5e3e::/64
# with netmask: fdfd:87b5:b475:5e3e::/ffff:ffff:ffff:ffff::
# with hostmask: fdfd:87b5:b475:5e3e::/::ffff:ffff:ffff:ffff
# num addresses: 18446744073709551616
Copy the code

Like addresses, there are two network classes for IPv4 and IPv6 networks. Each class provides properties or methods for accessing values associated with the network, such as broadcast addresses and addresses on the network available to hosts.

Network instances are iterative and generate addresses 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()
    
# output
# IPv4Network (' 10.9.0.0/24)
# 10.9.0.0
# 10.9.0.1
# 10.9.0.2
# 
# IPv6Network('fdfd:87b5:b475:5e3e::/64')
# fdfd:87b5:b475:5e3e::
# fdfd:87b5:b475:5e3e::1
# fdfd:87b5:b475:5e3e::2
Copy the code

This example prints only some addresses, because the IPv6 network can contain far more addresses than are in the output.

An iterating network generates addresses, but not all of them are valid for the host. For example, the base address and broadcast address of the network. To find addresses available to 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()
    
# output
# IPv4Network (' 10.9.0.0/24)
# 10.9.0.1
# 10.9.0.2
# 10.9.0.3
# 
# IPv6Network('fdfd:87b5:b475:5e3e::/64')
# fdfd:87b5:b475:5e3e::1
# fdfd:87b5:b475:5e3e::2
# fdfd:87b5:b475:5e3e::3
Copy the code

Comparing the output from this example with the previous example shows that the host address does not include the first value generated when iterating over the entire network.

In addition to the iterator protocol, the network supports the IN operation to determine whether an address is part of the 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()
    
# output
# 10.9.0.6
# is on 10.9.0.0/24
# 
# 10.7.0.31
# is not on a known network
# 
# fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa
# is on fdfd:87b5:b475:5e3e::/64
# 
# fe80::3840:c439:b25e:63b0
# is not on a known network
Copy the code

In uses network masks to test addresses, so it is more efficient than extending a full list of addresses on a network.

interface

A network interface represents a specific address on a network and can be represented by 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()
    
# output
# IPv4Interface (' 10.9.0.6/24)
# network:
# 10.9.0.0/24
# ip:
# 10.9.0.6
# IP with prefixlen:
# 10.9.0.6/24
# netmask:
# 10.9.0.6/255.255.255.0
# hostmask:
# 10.9.0.6/0.0.0.255
# 
# IPv6Interface('fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/64')
# network:
# fdfd:87b5:b475:5e3e::/64
# ip:
# fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa
# IP with prefixlen:
# fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/64
# netmask:
# fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/ffff:ffff:ffff:ffff::
# hostmask:
# fdfd:87b5:b475:5e3e:b1bc:e121:a8eb:14aa/::ffff:ffff:ffff:ffff
Copy the code

Interface objects have properties that access the entire network and address, respectively, and several different ways of expressing interfaces and network masks. Related documents:

Pymotw.com/3/ipaddress…