Python – Find IP Location with IPSTACK API (Geolocation)

# Author: Kerry Cordero
# Version: 1.0.0
# Description: This script will find IP Location via ipstack API

import requests
from prettytable import PrettyTable

api_key = "API_KEY"
ip_address = input("Enter the IP address: ")

url = f"http://api.ipstack.com/{ip_address}?access_key={api_key}"
response = requests.get(url)
data = response.json()

if "error" in data:
    print("Error:", data["error"]["info"])
else:
    location = {
        "IP": data["ip"],
        "Country": data["country_name"],
        "Region": data["region_name"],
        "City": data["city"],
        "Latitude": data["latitude"],
        "Longitude": data["longitude"]
    }

    # Create a PrettyTable instance
    table = PrettyTable()

    # Set the field names
    table.field_names = ["Attribute", "Value"]

    # Add rows
    for key, value in location.items():
        table.add_row([key, value])

    # Print the table
    print(table)

 

Replace API_KEY with your actual API key obtained from the ipstack website (https://ipstack.com/).