Skip to content

Instantly share code, notes, and snippets.

@nc9
Created November 24, 2025 23:51
Show Gist options
  • Select an option

  • Save nc9/387c38127bca3a63cb17556e42a1794f to your computer and use it in GitHub Desktop.

Select an option

Save nc9/387c38127bca3a63cb17556e42a1794f to your computer and use it in GitHub Desktop.
Command line script to get IP and geo info from cloudflare
#!/usr/bin/env -S uv --quiet run --script
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "requests",
# "pydantic",
# "rich",
# ]
# ///
"""
Script that obtains IP address and geo info and publishes to the console.
Uses Cloudflare's API to fetch geolocation information for the current public IP.
"""
import requests
from pydantic import BaseModel, Field
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
class IPInfo(BaseModel):
"""Pydantic model representing IP geolocation information from Cloudflare API."""
colo: str = Field(description="Cloudflare data center code (airport code)")
asn: int = Field(description="Autonomous System Number")
continent: str = Field(description="Two-letter continent code")
country: str = Field(description="Two-letter country code (ISO 3166-1 alpha-2)")
region: str = Field(description="Region or state name")
city: str = Field(description="City name")
latitude: str = Field(description="Latitude coordinate as string")
longitude: str = Field(description="Longitude coordinate as string")
ip_address: str = Field(description="Public IP address")
ip_version: str = Field(description="IP version (IPv4 or IPv6)")
def get_ip_info() -> IPInfo:
"""
Fetch IP geolocation information from Cloudflare's API.
Returns:
IPInfo: Parsed geolocation data for the current public IP.
Raises:
requests.HTTPError: If the API request fails.
"""
url = "https://ipv4-check-perf.radar.cloudflare.com/api/info"
response = requests.get(url)
response.raise_for_status()
return IPInfo.model_validate(response.json())
def display_ip_info(info: IPInfo) -> None:
"""
Display IP geolocation information in a formatted, colorful output.
Args:
info: The IPInfo object containing geolocation data.
"""
console = Console()
table = Table(show_header=False, box=None, padding=(0, 2))
table.add_column("Field", style="cyan bold")
table.add_column("Value", style="white")
table.add_row("🌐 IP Address", f"{info.ip_address} ({info.ip_version})")
table.add_row("πŸ“ Location", f"{info.city}, {info.region}")
table.add_row("🏳️ Country", f"{info.country} ({info.continent})")
table.add_row("🌍 Coordinates", f"{info.latitude}, {info.longitude}")
table.add_row("πŸ”— ASN", str(info.asn))
table.add_row("☁️ Cloudflare PoP", info.colo)
panel = Panel(
table,
title="[bold green]My IP Info[/bold green]",
border_style="green",
padding=(1, 2),
)
console.print(panel)
def main() -> None:
"""Main entry point for the script."""
ip_info = get_ip_info()
display_ip_info(ip_info)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment