How to get network address using ipaddress?

network = ipaddress.IPv4Network("")
print("Network address: ", network.network_address)
network = ipaddress.IPv4Network("")

File “/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/”, line 1454, in init
raise ValueError(’%s has host bits set’ % self)
ValueError: has host bits set

This returns an error. If I change the IP address to then it’ll return Isn’t this function supposed to return when I give it

The default is that strict=True

From ipaddress

class ipaddress. IPv4Network ( address , strict=True )
If strict is True and host bits are set in the supplied address, then ValueError is raised. Otherwise, the host bits are masked out to determine the appropriate network address.


>>> ipaddress.IPv4Network("", strict=False)

Thanks for the answer. I wish they would make an explanation that is easier to understand.