Python – Find IP Location with IPDATA 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"https://api.ipdata.co/{ip_address}?api-key={api_key}"
response = requests.get(url)
data = response.json()

if "message" in data:
    print("Error:", data["message"])
else:
    location = {
        "IP": data["ip"],
        "Country": data["country_name"],
        "Region": data["region"],
        "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 ipdata website (https://ipdata.co/).