How to Create a Simple IP Tracker using Python

 Humans cannot be separated from curiosity. Curiosity is a basic human trait. There are many things that can be done to satisfy curiosity. Technological developments have also contributed to making it easier for humans to satisfy their curiosity. As is the case today, we can create an IP tracker / IP tracker ourselves instead of searching on the internet.

We will try to make an IP tracker by utilizing the API from the ipapi.co which is provided for free. For the programming language this time we will try to use Python. ipapi.co provides responses in several formats, namely JSON, JSONP, XML, CSV, and YAML. To make it easier for us, we will use the JSON format.

How to Create a Simple IP Tracker using Python
How to Create a Simple IP Tracker using Python

Before starting, as a disclaimer, I am not responsible for the misuse of this simple IP tracker. I only share knowledge that I think can be useful for many people.

Please note that the IP address information can be found with the URL format ipapi.co/ip/json . For example, if I enter a random IP address, the detailed information of that IP address will appear. 

What needs to be done first is to import the json module and request package urllib . 

import json 
from urllib import request 

After that we wrap the API url and user input into url and ip variables.

url = "https://ipapi.co/" 
ip = input("Input the IP Address : ") 

Then, we use the urlopen module request and the loads module json to read the data.

request = request.urlopen(url + ip + "/json") 
data_json = json.loads(request.read()) 

After that, we just need to display detailed information from the IP address that will be inputted by the user such as country, province, city, to postal code.

print("IP : " + str(data_json['ip']))
print("Country : " + str(data_json['country_name']))
print("Region : " + str(data_json['region']))
print("City : " + str(data_json['city']))
print("Postal Code : " + str(data_json['postal']))
print("Latitude : " + str(data_json['latitude']))
print("Longitude : " + str(data_json['longitude']))
print("ISP : " + str(data_json['org']))

So, the full code is something like this.


#!/usr/bin/python

import json
from urllib import request

print("\n== IP Tracker ==\n")
url = "https://ipapi.co/"
ip = input("Input the IP Address : ")
request = request.urlopen(url + ip + "/json")
data_json = json.loads(request.read())

print("IP : " + str(data_json['ip']))
print("Country : " + str(data_json['country_name']))
print("Region : " + str(data_json['region']))
print("City : " + str(data_json['city']))
print("Postal Code : " + str(data_json['postal']))
print("Latitude : " + str(data_json['latitude']))
print("Longitude : " + str(data_json['longitude']))
print("ISP : " + str(data_json['org']))

If we run, it will look like the below.



If writing prints one by one is too long and time-consuming, we can shorten the code by doing a simple loop using for. The full code will look like this.


#!/usr/bin/python

import json
from urllib import request

print("\n== IP Tracker ==\n")
url = "https://ipapi.co/"
ip = input("Input the IP Address : ")
request = request.urlopen(url + ip + "/json")
data_json = json.loads(request.read())

for data in data_json:
	info = str(data_json[data])
	print(data + " : " + info)

And if we run, roughly it will look like this.


That's how to make a simple IP tracker using the Python programming language.

Hopefully, this article can be useful. 

Post a Comment

Sponsored