In this article, we will learn how to check if an IPv4 IP address is local(private) or not in python. There are various ways to check if the IPv4 IP is private or not.
Here are some examples to check if the IPv4 IP address is private or Not.
In this example, we used the ipaddress
module. The ipaddress
module allows you to do just that by viewing and manipulating IP addresses as Python objects.
Here is the source code of the program to check if the IPv4 IP address is Local(Private) or not.
# How to Check if the IPv4 IP Address is Local(Private) or Not in Python
# Importing ip_address from
# ipaddress module
from ipaddress import ip_address
def IPAddress(IP: str) -> str:
return str(IP) + " is Local(Private) IP Address" if (ip_address(IP).is_private) else str(IP) + " is not Local(private) IP Address"
if __name__ == '__main__' :
# Print Output
# Public IP
print(IPAddress('17.5.7.8'))
print(IPAddress('152.168.0.120'))
# Private IP
print(IPAddress('172.16.0.10'))
print(IPAddress('127.0.0.1'))
print(IPAddress('192.168.0.12'))
17.5.7.8 is not Local(private) IP Address
152.168.0.120 is not Local(private) IP Address
172.16.0.10 is Local(Private) IP Address
127.0.0.1 is Local(Private) IP Address
192.168.0.12 is Local(Private) IP Address
In this example, we used the struct
and socket
module. The struct
module performs conversions between Python values and C structs represented as Python bytes objects. This can be used in handling binary data stored in files or from network connections, among other sources. And the socket
module provides access to the BSD socket
interface. It is available on all modern Unix systems, Windows, macOS, and probably additional platforms.
Here is the source code of the program to check if the IPv4 IP address is Local(Private) or not.
# How to Check if the IPv4 IP Address is Local(Private) or Not in Python
# Import Module
import struct
import socket
def _is_private_ip(ip):
"""Check if the IP belongs to private network blocks.
@param ip: IP address to verify.
@return: boolean representing whether the IP belongs or not to
a private network block.
"""
networks = [
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.88.99.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"240.0.0.0/4",
"255.255.255.255/32",
"224.0.0.0/4",
]
for network in networks:
try:
ipaddr = struct.unpack(">I", socket.inet_aton(ip))[0]
netaddr, bits = network.split("/")
network_low = struct.unpack(">I", socket.inet_aton(netaddr))[0]
network_high = network_low | 1 << (32 - int(bits)) - 1
if ipaddr <= network_high and ipaddr >= network_low:
return str(ip) + " is Local(Private) IP Address"
except:
continue
return str(ip) + " is not Local(private) IP Address"
# Print Output
# Public IP
print(_is_private_ip('17.5.7.8'))
print(_is_private_ip('152.168.0.120'))
# Private IP
print(_is_private_ip('172.16.0.10'))
print(_is_private_ip('127.0.0.1'))
print(_is_private_ip('192.168.0.12'))
17.5.7.8 is not Local(private) IP Address
152.168.0.120 is not Local(private) IP Address
172.16.0.10 is Local(Private) IP Address
127.0.0.1 is Local(Private) IP Address
192.168.0.120 is Local(Private) IP Address
In this example, we used the regular expression to check if the IPv4 IP Address is local(private) or not.
Here is the source code of the program to check if the IPv4 IP address is Local(Private) or not.
# How to Check if the IPv4 IP Address is Local(Private) or Not in Python
# Import re Module for Regular Expression
import re
# Define a Function
def is_ip_private(ip):
# https://en.wikipedia.org/wiki/Private_network
priv_lo = re.compile("^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
priv_24 = re.compile("^10\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
priv_20 = re.compile("^192\.168\.\d{1,3}.\d{1,3}$")
priv_16 = re.compile("^172.(1[6-9]|2[0-9]|3[0-1]).[0-9]{1,3}.[0-9]{1,3}$")
res = priv_lo.match(ip) or priv_24.match(ip) or priv_20.match(ip) or priv_16.match(ip)
if(res):
return str(ip) + " is Local(Private) IP Address"
else:
return str(ip) + " is not Local(Private) IP Address"
# Print Output
# Public IP
print(is_ip_private('17.5.7.8'))
print(is_ip_private('152.168.0.120'))
# Private IP
print(is_ip_private('172.16.0.10'))
print(is_ip_private('127.0.0.1'))
print(is_ip_private('192.168.0.12'))
17.5.7.8 is not Local(Private) IP Address
152.168.0.120 is not Local(Private) IP Address
172.16.0.10 is Local(Private) IP Address
127.0.0.1 is Local(Private) IP Address
192.168.0.12 is Local(Private) IP Address
I hope this article will help you to understand how to check if an IPv4 IP address is local(private) or not in python.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments