How to Get IPv4/IPv6 Address Range from CIDR in Python?

CIDR is short for Classless Inter-Domain Routing. It is used for allocating IP addresses and for IP routing. The CIDR number usually comes after the IP address and is preceded by a slash "/". For example, would be expressed as using a subnet mask of (which has 24 network bits).

To get the starting and ending IP range from CIDR in Python, we can use Python's built-in module called ipaddress as shown in the example below:

import ipaddress

def get_ip_range(cidr):
    net = ipaddress.ip_network(cidr)
    return net[0], net[-1]

cidr = ""
ip_range = get_ip_range(cidr)
start_ip = ip_range[0]
end_ip = ip_range[1]
print("Start ip = ", start_ip)
print("End ip = ", end_ip)

The output of the above code is as follows:

Start ip =
End ip =