March 2019
Beginner
490 pages
12h 40m
English
If you are wondering how to pick up a suitable IP subnet, try the ipaddress module. The following code snippet shows an example of how to choose a specific subnet, based on the number of necessary host IP addresses for a small private network.
Suppose you have a CIDR network address, such as 192.168.0.0/24, and you want to generate a range of all the IP addresses that it represents (192.168.0.1 to 192.168.0.254). The ipaddress module can be easily used to perform such calculations:
>>> import ipaddress >>> net = ipaddress.ip_network('192.168.0.0/24') >>> net IPv4Network('192.168.0.0/24') >>> for a in net: ... print(a) ... 192.168.0.1 192.168.0.2 192.168.0.3 ... 192.168.0.254
In this example, ...
Read now
Unlock full access