Python – check if ip is public or private

 

netaddr is a Python library for representing and manipulating network addresses.

Installing netaddr library:

Use any of following command to install netaddr library

$ sudo pip install netaddr
or
$ easy_install netaddr

Observe following example for more details:

>>> from netaddr import *
>>> IPAddress('74.125.236.194').is_private()
False
>>> IPAddress('192.168.1.10').is_private()
True
>>> IPAddress('127.0.0.1').is_loopback()
True

IPAddress(‘input ip’).is_private() will return true if the input ip address private, else it will return false.

Reference: Netaddr

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s