The iplib module contains functions, classes, and constants useful for managing IPv4 addresses and netmasks.
You can use this Python module (and the included scripts ipconv, nmconv, and cidrinfo) to convert amongst many different notations and to manage address/netmask pairs in CIDR notation.
iplib supports Python 3.10 through Python 3.16+ and includes complete type annotations.
These functions always return True or False (never raise exceptions) and can be called with any kind of arguments (integers, strings, and objects with a __str__() method which returns a suitable string):
is_dot(ip): ReturnTrueif the IP address is in dotted decimal notation.is_hex(ip): ReturnTrueif the IP address is in hexadecimal notation.is_bin(ip): ReturnTrueif the IP address is in binary notation.is_oct(ip): ReturnTrueif the IP address is in octal notation.is_dec(ip): ReturnTrueif the IP address is in decimal notation.is_dot_nm(nm): ReturnTrueif the netmask is in dotted decimal notation.is_hex_nm(nm): ReturnTrueif the netmask is in hexadecimal notation.is_bin_nm(nm): ReturnTrueif the netmask is in binary notation.is_oct_nm(nm): ReturnTrueif the netmask is in octal notation.is_dec_nm(nm): ReturnTrueif the netmask is in decimal notation.is_bits_nm(nm): ReturnTrueif the netmask is in bits notation.is_wildcard_nm(nm): ReturnTrueif the netmask is in wildcard bits notation.
Functions to detect IP/netmask notation; return IP_UNKNOWN/NM_UNKNOWN if the IP/netmask notation is not detected:
detect(ip): Try to detect the notation of an IP address.detect_nm(nm): Try to detect the notation of a netmask.p_detect(ip)andp_detect_nm(nm): detect the notation of an IP address (or netmask) and return a nice string ('unknown' if it's not detected).is_notation(ip, notation)andis_notation_nm(nm, notation): returnTrueif the given IP address (or netmask) is in the specified notation.
Functions to convert IP/netmask; can raise a ValueError exception:
convert(ip, notation=IP_DOT, inotation=IP_UNKNOWN)andconvert_nm(nm, notation=IP_DOT, inotation=IP_UNKNOWN): Convert the given IP address (or netmask) to the given notation; thenotationargument sets the notation of the output; theinotationargument forces the input to be considered as an address in the specified notation. When the IP address (or netmask) is an integer, theinotationargument is assumed to beIP_DEC(if not set otherwise).
IPv4Address(ip, notation=IP_UNKNOWN): This class represents an IPv4 Internet address.
An IPv4Address object can be used to sum or subtract two IP addresses or an integer offset:
>>> import iplib
>>> ip = iplib.IPv4Address('127.0.0.1')
>>> ip + 1000
<IPv4 address 127.0.3.233>It's also possible to compare two IP addresses:
>>> iplib.IPv4Address('127.0.0.1') < iplib.IPv4Address('127.0.0.4')
TrueFor both IPv4Address and IPv4NetMask objects you can specify the notation:
>>> iplib.IPv4Address('24323', iplib.IP_OCT)
>>> iplib.IPv4Address('24323', 'oct')
>>> iplib.IPv4Address('24323', 'octal')IPv4NetMask(nm, notation=IP_UNKNOWN): This class represents an IPv4 Internet netmask.
CIDR(ip, netmask=None): The representation of a Classless Inter-Domain Routing (CIDR) address.
From instances of this class, you can retrieve information about usable IP addresses, broadcast, and network address:
>>> cidr = iplib.CIDR('127.0.0.1/8')
>>> cidr.is_valid_ip('127.4.5.6')
TrueThe following constants are used to define IP/netmask notations:
IP_DOTandNM_DOT: dotted decimal notation (e.g.:192.168.0.42).IP_HEXandNM_HEX: hexadecimal notation (0xC0A8002A).IP_BINandNM_BIN: binary notation (11000000101010000000000000101010).IP_OCTandNM_OCT: octal notation (0o30052000052).IP_DECandNM_DEC: decimal notation (3232235562).NM_BITS: netmask in bits notation (24).NM_WILDCARD: netmask in wildcard bits notation (0.0.0.255).IP_UNKNOWNandNM_UNKNOWN: unknown notation.NOTATION_MAP: a dictionary mapping notations to valid string identifiers.VALID_NETMASKS: a dictionary mapping valid netmask bit lengths to decimal values.
This code is released under the BSD license.
Copyright 2001-2026 Davide Alberani da@mimante.net