How to get network address using ipaddress?

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

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

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

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.

So:

>>> ipaddress.IPv4Network("192.168.1.1/24", strict=False)
IPv4Network('192.168.1.0/24')

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