IP address parsing for humans.
Cidrize takes IP address inputs that people tend to use in practice, validates them, and converts them to objects.
Intelligently parses IPv4/IPv6 addresses, CIDRs, ranges, and wildcard matches to attempt return a valid list of IP addresses.
cidrize() function does all the work trying to parse IP addresses correctly.
You can install
cidrize via Pip:
pip install cidrize
Cidrize is basically a thin veneer around netaddr to provide a human layer for parsing IP addresses.
Supported input formats
Input is very flexible and can be of any of the following formats:
192.0.2.18 22.214.171.124/26 192.0.2.80-192.0.2.85 192.0.2.170-175 192.0.2.8[0-5] 192.0.2.
Hyphenated ranges do not need to form a CIDR block but the starting number must
be of lower value than the end. The
netaddr module does most of the heavy
lifting for us here.
Network mask (e.g. 192.0.2.0 255.255.255.0) and host mask (aka reverse mask, 192.0.2.0 0.0.0.255) notation are not accepted at this time.
The cidrize function returns a list of consolidated
objects. By default parsing exceptions will raise a
default argument of
raise_errors=True). You may pass
raise_errors=False to cause
exceptions to be stripped and the error text will be returned as a list. This
is intended for use with scripts or APIs where receiving exceptions would not
The module may also be run as a script for debugging purposes.
The cidrize function
Fire up your trusty old Python interpreter and follow along!
>>> from cidrize import cidrize
>>> cidrize("126.96.36.199") [IPNetwork('188.8.131.52/32')]
Hyphenated range (default, strict=False)
>>> cidrize("184.108.40.206-220.127.116.11") [IPNetwork('18.104.22.168/25')]
Hyphenated range strict (strict=True)
>>> cidrize("22.214.171.124-126.96.36.199", strict=True) [IPNetwork('188.8.131.52/29'), IPNetwork('184.108.40.206/28'), IPNetwork('220.127.116.11/27'), IPNetwork('18.104.22.168/28'), IPNetwork('22.214.171.124/32')]
You may provide wildcards using asterisks. This is limited to the 4th and final octet only:
>>> cidrize("15.63.148.*") [IPNetwork('126.96.36.199/24')]
>>> cidrize("188.8.131.52[40-99]") [IPNetwork('184.108.40.206/30'), IPNetwork('220.127.116.11/28'), IPNetwork('18.104.22.168/27'), IPNetwork('22.214.171.124/29')]
Bad CIDR prefixes are rejected outright:
>>> cidrize("126.96.36.199/40") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "cidrize.py", line 145, in cidrize raise CidrizeError(err) cidrize.CidrizeError: CIDR prefix /40 out of range for IPv4!
Ranges must always start from lower to upper bound, or this happens:
>>> cidrize("188.8.131.52-0") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "cidrize.py", line 145, in cidrize raise CidrizeError(err) cidrize.CidrizeError: lower bound IP greater than upper bound!
The cidrize package also comes with the
cidr command, which has two basic operations.
% cidr 184.108.40.206/30 220.127.116.11/30
% cidr -v 18.104.22.168/30 Spanning CIDR: 22.214.171.124/30 Block Start/Network: 126.96.36.199 1st host: 188.8.131.52 Gateway: 184.108.40.206 Block End/Broadcast: 220.127.116.11 DQ Mask: 255.255.255.252 Cisco ACL Mask: 0.0.0.3 # of hosts: 2 Explicit CIDR blocks: 18.104.22.168/30
And that's that!
Cidrize is licensed under the BSD 3-Clause License. Please see
for the details.