简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM