The ipaddress module parses IPv4, IPv6, and CIDR networks without third-party dependencies. Normalized objects support membership tests, version checks, and properties such as private, global, or loopback.
from ipaddress import ip_address, ip_network
client = ip_address("192.0.2.25")
allowed = ip_network("192.0.2.0/24")
if client in allowed:
print("allowed")
print(client.version, client.is_private)
How to use it safely
Handle ValueError for external input and decide whether a network with host bits should be rejected or normalized through strict=False. Classification properties aid analysis, but they do not replace firewall rules or trustworthy proxy configuration.
To strengthen the foundation, read Python collections guide and type hints guide.
The official Python documentation, accessed July 22, 2026, describes the API, edge cases, and version compatibility.