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):

  • 10.0.0.0 to 10.255.255.255 (Class A)

  • 172.16.0.0 to 172.31.255.255 (Class B)

  • 192.168.0.0 to 192.168.255.255 (Class C)

  • 127.0.0.0 to 127.255.255.255 (Loopback)

Python's ipaddress module provides a simple way to determine if an IP address is public or private using the is_private attribute.

Approach

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

  • Import the ipaddress module

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

  • Create an ipaddress object using ipaddress.ip_address(ip)

  • Check if the IP address is private using the is_private attribute

  • Handle exceptions with try-except for invalid IP addresses

  • Return the result

Basic IP Address Check

This example demonstrates a simple function to check if an IP address is 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)
192.168.0.1 is Private IP: True

Complete Function with Error Handling

This enhanced version includes proper error handling for invalid IP addresses:

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"

# Test multiple IP addresses
ip_addresses = ['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', '256.1.1.1']

for ip in ip_addresses:
    result = check_ip_address(ip)
    print("IP Address: {} ? {}".format(ip, result))
IP Address: 127.0.0.1 ? Private IP
IP Address: 10.98.76.6 ? Private IP
IP Address: 17.5.7.8 ? Public IP
IP Address: 192.168.0.1 ? Private IP
IP Address: 8.8.8.8 ? Public IP
IP Address: 10.0.0.1 ? Private IP
IP Address: 172.16.0.0 ? Private IP
IP Address: 256.1.1.1 ? Invalid IP

Key Points

  • The is_private attribute automatically checks against all reserved private IP ranges

  • Loopback addresses (127.x.x.x) are considered private

  • Always use try-except blocks to handle invalid IP addresses gracefully

  • The module supports both IPv4 and IPv6 addresses

Conclusion

The Python ipaddress module provides a reliable way to classify IP addresses as public or private. Using the is_private attribute with proper error handling ensures robust IP address validation and classification in your applications.

Updated on: 2026-03-27T13:54:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements