IP address objects

In this example, the loopback interface is defined with the 127.0.0.1 IP address. As you can see, the is_loopback property returns True:

>>> loopback = ipaddress.IPv4Interface('127.0.0.1')
>>> loopback.is_private
True
>>> loopback.is_reserved
False
>>> loopback.is_multicast
False
>>> loopback.is_loopback
True

The IP address classes have many more interesting properties. You can perform some arithmetic and logical operations on those objects. For example, we can check whether an IP address is part of a network.

In this example, we are checking whether an IP address is part of a specific network. Here, a network called net has been defined by the network address, which is 192.168.1.0/24, and the membership of eth0 and eth1 has been tested to see if these IP addresses are part of the network:

>>> eth0 = ipaddress.IPv4Interface('192.168.1.1')
>>> eth1 = ipaddress.IPv4Interface('192.168.2.1')
>>> net = ipaddress.ip_network('192.168.1.0/24')
>>> eth0 in net
True
>>> eth1 in net
False
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset