How To Check Whether Ip Is Private or Public in python?

 

thumbnail


Welcome Coders, In this tutorial we are going to learn that how we can check whether an Ip address is public or private.

You can easily do this by using ipaddress module of python.

What is Ip address?

Ip address or Internet Protocol address is a unique address that is assigned to each devices that are connected to a network. It is a string of numbers separated by periods and expressed as set of four number whose range is from 0 to 255.
Example-192.164.16.101

Public Ip Address:

Public Ip address is a Ip address  that is assigned by Internet Service Provider(ISP).Its scope is local and is used to communicate outside the network.
You can find your public Ip address by searching "what is my ip" on google.

Private Ip Address:

Private Ip address is a Ip address that is used to communicate within the same network. Its scope is local and its work only in local area network(LAN).

you can find your Private Ip address by writing "ipconfig" on command prompt.


About Ip Address Module:

ipaddress provides the capabilities to create ,manipulate and operate on IPv4 and IPv6 address and networks.
Use pip Install ipaddress if not available in your python version.

Code To Check Private or Public Ip Address

Source Code:

from ipaddress import *

def Check(Ip:str):
    """Function To Check Whether Ip address
    is Private or Public
    """
    if ip_address(Ip).is_private:
        return "This Ip Address is Private"
    else:
        return "This Ip Address is Public"

print(Check("Ip adderess to check"))

Do Follow Us On Instagram To Stay In Touch With Us.
@community_programmer
https://www.instagram.com/community_programmer/

0 Comments

Newest