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 ip-api.com JSON API

    url = "http://ip-api.com/json/"

    

    # Send a request to the ip-api.com API to retrieve the geolocation information

    response = urllib2.urlopen(url + ip)

    

    # Read the response data

    data = response.read()

    

    # 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

    break

#clcoding.com 

Explanation: 

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 ip-api.com 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 ip-api.com 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 ip-api.com JSON API is constructed as http://ip-api.com/json/.

A request is made to the ip-api.com 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 ip-api.com API.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (13) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (161) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (53) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (917) Python Coding Challenge (304) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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