Romain's blog

ipaddress module: IPv4/IPv6 manipulation like a boss

If you are not a network engineer having to deal with IP adresses and IP networks (like me), you might be interested by this post.

I recently discovered a very convenient Python module called ipaddress. It's a no-brainer for common questions.

from ipaddress import ip_address, ip_network

ip_address("192.168.0.16") in ip_network("192.168.0.0/28")
# False
ip_network("192.168.0.0/28").subnet_of(ip_network("192.168.0.0/22"))
# True
ip_network("192.168.0.0/22").num_addresses
# 1024
# I also know the formulae 2^(32-n)
2 ** (32-22)
# 1024

#network #python