Edamame Edamame - 9 days ago 5
Python Question

python: ipaddress package output not consistent with CIDR website

I am converting some CIDR using python ipaddress package like:

import ipaddress
net4 = ipaddress.ip_network(u'10.120.160.0/19')
for x in net4.hosts():
print(x)


The first_ip output from the above code is
10.120.160.1
, while the last_ip from the above code is
10.120.191.254


However, if I use the website: http://www.ipaddressguide.com/cidr to do the conversion: the first_ip becomes
10.120.160.0
while the last_ip becomes
10.120.191.255


Does anyone know why the results are different and which one is correct? Thanks!

Answer

The last address will be the subnet's broadcast address, so you can't give it to a host, and the first is the network's address itself, which is also not assigned to hosts. So both sources are correct, they just tell you different things: the python routine gives you IPs suitable for hosts, while the web page gives you all IPs in the range, including network and broadcast.