IP network objects

When working with an IP address that represents a network, we could work with an IPv4Network or IPv6Network object depending on the IP address passed as the argument. For this task, we can use the ip_network() method from the ipaddress module using as parameter a string or integer representing the IP network.

Let's import the ipaddress module and define a net6 network:

 >>> import ipaddress
>>> net6 = ipaddress.ip_network('2001:db8::/48')

Now, we get some useful information, such as version, netmask, and the network/broadcast address:

>>> net6.version
6
>>> net6.netmask
IPv6Address('ffff:ffff:ffff::')

Similarly, you can find the network and the broadcast addresses of net6 by doing the following:

 >>> net6.network_address
IPv6Address('2001:db8::')
>>> net6.broadcast_address
IPv6Address('2001:db8:0:ffff:ffff:ffff:ffff:ffff')

Also, we can get the number of addresses net6 can hold:

>>> net6.num_addresses
1208925819614629174706176
..................Content has been hidden....................

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