Python program to determine if the given IP Address is Public or Private using ipaddress module


In computer networking, IP addresses are used to uniquely identify devices connected to a network. IP addresses can be classified as either public or private.

Public IP addresses are assigned to devices that are directly connected to the Internet. They are globally routable and can be accessed from anywhere on the Internet. 

Private IP addresses, on the other hand, are used within private networks, such as local area networks (LANs) or home networks. These IP addresses are not directly accessible from the Internet. Private IP addresses are defined by certain reserved address ranges specified by the Internet Engineering Task Force (IETF). This allows multiple devices within a private network to use the same IP address ranges without conflicts.

To determine if an IP address is public or private programmatically, we can use the ipaddress module in Python. This module provides classes and functions for working with IP addresses. 

By creating an ipaddress object from the given IP address, we can check if it belongs to a reserved private IP address range using the is_private attribute. If the IP address is within a private range, it is considered a private IP address. Otherwise, it is considered a public IP address.

In this article, we will discuss how to use the ipaddress module to determine if the given IP Address is Public or Private.

Approach

Here are the steps to determine if a given IP address is public or private using the ipaddress module in Python &minus

  • Import the ipaddress module

  • Create a function that takes an IP address as an input parameter.

  • Create an ipaddress object: Within the function, attempt to create an ipaddress object using the provided IP address. This can be done by calling ipaddress.ip_address(ip).

  • Check if the IP address is private: Use the is_private attribute of the ipaddress object to determine if the IP address falls within a reserved private IP address range. If is_private is True, the IP address is private. Otherwise, it is public.

  • Handle exceptions: To handle cases where an invalid IP address is provided, wrap the creation of the ipaddress object in a try-except block. If a ValueError is raised, catch the exception and handle it accordingly. Return an "Invalid IP" message to indicate that the provided IP address is not valid.

  • Return the result.

Example

In this example, we will use the ipaddress.ip_address function to determine if the given IP Address is Public or Private.

import ipaddress

def check_ip_address(ip):
    return ipaddress.ip_address(ip).is_private

ip_address = '192.168.0.1'
is_private = check_ip_address(ip_address)
print("{} is Private IP:".format(ip_address), is_private)

Output

192.168.0.1 is Private IP: True

Example

In this example, if the provided IP address is invalid and cannot be used to create an ipaddress object, a ValueError will be raised.

To handle this scenario, we include a try-except block in the code. If a ValueError is raised when attempting to create the ip_obj, the program catches the exception and returns "Invalid IP" to indicate that the provided IP address is not valid.

import ipaddress

def check_ip_address(ip):
    try:
        ip_obj = ipaddress.ip_address(ip)
        if ip_obj.is_private:
            return "Private IP"
        else:
            return "Public IP"
    except ValueError:
        return "Invalid IP"

# Define ip_address
ip_address = ['127.0.0.1', '10.98.76.6', '17.5.7.8', '192.168.0.1', "8.8.8.8", "10.0.0.1", '172.16.0.0']
for ip in ip_address:
    result = check_ip_address(ip)
    print("IP Address:", ip)
    print("Result:", result)
    print()

Output

IP Address: 127.0.0.1
Result: Private IP

IP Address: 10.98.76.6
Result: Private IP

IP Address: 17.5.7.8
Result: Public IP

IP Address: 192.168.0.1
Result: Private IP

IP Address: 8.8.8.8
Result: Public IP

IP Address: 10.0.0.1
Result: Private IP

IP Address: 172.16.0.0
Result: Private IP

Updated on: 29-Aug-2023

508 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements