I'm setting a filter that runs some code if an IPv4 Address is not in an address range, I'm using ipaddress python3's module, but this is confusing me a lot, because it says that 172.16.32.5 is in the CIDR 172.16.0.0/12, when this is false:
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ipaddress import IPv4Address,IPv4Network
>>> ip_addr = IPv4Address('172.16.32.5')
>>> ip_net = IPv4Network('172.16.0.0/12')
>>> all_addr = list(ip_net.hosts())
>>> ip_addr in ip_net
True
>>> all_addr[-1]
IPv4Address('172.31.255.254')
As you can see, the ip_addr is beyond the last host in that network...
What I'm doing wrong? Is there a bug in this module?
A /12 subnet mask means the first 12 bits are set in the binary view. The subnet and the host you are querying have the same first 12 bits:
The last one is what I think you are misreading it as, which has different first 12 bits, and would be outside the subnet.
See also: http://jodies.de/ipcalc?host=172.16.0.0&mask1=12&mask2=