Python program to determine if the given IPv4 Address is reserved using ipaddress module


In the traditional IPv4 addressing scheme, IP addresses are divided into five classes: A, B, C, D, and E. Class E addresses, ranging from 240.0.0.0 to 255.255.255.255, are designated for particular purposes and are not intended for general use in the current internet infrastructure. As a result, Class E addresses are considered "reserved" and are not allocated or routable on the public internet.

To determine if a given IPv4 address falls within one of the reserved IP address ranges defined by organizations like the Internet Engineering Task Force (IETF) and the Internet Assigned Numbers Authority (IANA), Python utilizes the is_reserved attribute of the IPv4Address object provided by the ipaddress module. By checking this attribute, we can accurately identify whether the given address is reserved or not.

Input Output scenarios

Let’s see a few input-output scenarios for the Python program that determines if a given IPv4 address is reserved using the ipaddress module:

Scenario 1 −

Input: "192.168.0.1"
Output: The address 192.168.0.1 is not reserved.

Scenario 2 −

Input: "10.0.0.1"
Output: The address 10.0.0.1 is reserved.

Scenario 3 −

Input: "172.16.0.1"
Output: The address 172.16.0.1 is not reserved.

Scenario 4 −

Input: "240.10.0.1"
Output: The address 240.10.0.1 is reserved.

Approach

We can follow the below steps to determine if a given IPv4 address is reserved using the ipaddress module in Python.

  • Import the ipaddress module.

  • Define a list with some example IPv4 addresses.

  • Iterate over each IPv4 address of the list and create an IPv4Address object using the ip_address() method.

  • And finally use the is_reserved attribute of the IPv4Address object to determine if the address is reserved or not.

The ipaddress.ip_address() method

It is used to create an IPv4Address or IPv6Address object representing an IP address. The method returns an IPv4Address or IPv6Address object, depending on the type of address provided. If the input address is invalid, such as an incorrect format or an out-of-range value, a ValueError is raised. Following is the syntax of the ip_address() method:

ipaddress.ip_address(address)

Where, the address parameter is a string representation of the IP address that we want to create an object.

The is_reserved attribute 

It is a property of the IPv4Address object in the ipaddress module. It is used to determine if an IP address is reserved or not based on the reserved IP address ranges defined by various organizations such as IETF and IANA.

It returns True if the IP address is reserved. Otherwise, it returns False.

Example

Let’s take an example and see how to determine if a given IPv4 address is reserved or not using the ipaddress module in Python.

import ipaddress

# define a list of ipv4 addresses 
ipv4_addresses = [
    "240.10.0.1",
    "10.0.0.1",   
    "172.16.0.1", 
    "192.168.0.1",
    "241.0.0.133",
]

# Check if each IPv4 address is reserved or not
for address in ipv4_addresses:
    ip = ipaddress.ip_address(address)
    if ip.is_reserved:
        print("The address {} is reserved.".format(address))
    else:
        print("The address {} is not reserved.".format(address))

Output

The address 240.10.0.1 is reserved.
The address 10.0.0.1 is not reserved.
The address 172.16.0.1 is not reserved.
The address 192.168.0.1 is not reserved.
The address 241.0.0.133 is reserved.

Example

In this example, we will use a try-except block to handle any invalid address input.

import ipaddress

# define a list of ipv4 addresses 
ipv4_addresses = [
    "240.10.0.1",
    "10.0.0.1",   
    "172.16.0.1", 
    "192.168.0.1",
    "241.0.0.133",
    "0.3.12"
]

# Check if each IPv4 address is reserved or not
for address in ipv4_addresses:
    try:
        ip = ipaddress.ip_address(address)
        if ip.is_reserved:
            print("The address {} is reserved.".format(address))
        else:
            print("The address {} is not reserved.".format(address))
    except ValueError:
        print("The address {} is not a valid IPv4 address.".format(address))

Output

The address 240.10.0.1 is reserved.
The address 10.0.0.1 is not reserved.
The address 172.16.0.1 is not reserved.
The address 192.168.0.1 is not reserved.
The address 241.0.0.133 is reserved.
The address 0.3.12 is not a valid IPv4 address.

Updated on: 29-Aug-2023

93 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements