Monday 8 April 2024

IP Address Infromation using Python


Code : 

import os

import urllib.request as urllib2

import json

# Use a while loop to continuously prompt the user for the target IP address

while True:

    # Prompt the user to input the target IP address

    ip = input("What is your target IP: ")


    # Construct the URL for the JSON API

    url = ""


    # Send a request to the API to retrieve the geolocation information

    response = urllib2.urlopen(url + ip)


    # Read the response data

    data =


    # Parse the JSON response data into a Python dictionary

    values = json.loads(data)


    # Print the geolocation information for the target IP address

    print("IP: " + values["query"])

    print("City: " + values["city"])

    print("ISP: " + values["isp"])

    print("Country: " + values["country"])

    print("Region: " + values["region"])

    print("Timezone: " + values["timezone"])


    # Exit the loop after printing the information once



This code snippet is a Python script that continuously prompts the user to input a target IP address and then retrieves geolocation information for that IP address using the JSON API. Let's break down each part of the code:

import os: This imports the os module, which provides a way to interact with the operating system. However, it's not used in this particular snippet.

import urllib.request as urllib2: This imports the urllib.request module under the alias urllib2. This module provides functionality for making HTTP requests, which is used to fetch data from the API.

import json: This imports the json module, which provides functions for encoding and decoding JSON data.

The code then enters a while True loop, which means it will continue to run indefinitely until explicitly stopped.

Inside the loop, the user is prompted to input a target IP address using the input() function. The IP address is stored in the variable ip.

The URL for the JSON API is constructed as

A request is made to the API using urllib2.urlopen(), passing in the constructed URL along with the target IP address.

The response data is read using the read() method of the response object.

The JSON response data is parsed into a Python dictionary using json.loads(), storing the result in the variable values.

The geolocation information extracted from the JSON response is printed to the console, including the IP address, city, ISP, country, region, and timezone.

Finally, the loop breaks after printing the information once, effectively ending the program.

This script allows users to input a target IP address and quickly obtain geolocation information about it using the API.


Post a Comment

Popular Posts


AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (118) C (77) C# (12) C++ (82) Course (62) Coursera (180) Cybersecurity (22) data management (11) Data Science (95) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (4) Pandas (3) PHP (20) Projects (29) Python (753) Python Coding Challenge (230) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)


Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses