tools / net / ip-location.py

net/ip-location.py

netinternet● networkpython3

Where an IP address is, from ip-api.com (no API key; its free tier is plain HTTP, so treat the answer as a hint, not a secret). Prints one line "City, Region, Country (CC) lat,lon tz" -- the City is the shape weather/current.py, weather/forecast.py and web/search.py take as their location. Pass ip= to look up a specific address; omitted, the caller's own public address is located.

Use when a workflow must find out where it is running (which city) before fetching local weather or news, or when the user asks where an IP address is.

Run it

1. Once per machine: install fragletc (Docker must already be running).

curl -fsSL https://raw.githubusercontent.com/ofthemachine/fraglet/main/install.sh | sh

2. Fetch the tool and print its contract.

curl -fsSLO https://tools.ofthemachine.com/net/ip-location.py && chmod +x ip-location.py && ./ip-location.py --fraglet-help

3. Run it.

./ip-location.py [-p ip="<ip>"]

Parameters

nametyperequirementdescription
ipstringoptionalIPv4 or IPv6 address to locate; omitted, the caller's own public address

Outputs

stdout

Provenance

image
ofthemachine/python3@sha256:a015b4f9f0e7c1648f8fcd4164f6a296dd3c9189404f88f333a9764d08d13522
reach
network=required — output depends on live external state; do not cache indefinitely.
procedure_hash
sha256:51d26958a410a6a3037932f4c540458646661cd691c6b67a6b490896afc0916d
receipt
add --receipt run.json to any run; receipt/attest.py verifies it against this hash without re-running.

Source

ip-location.py · 28 lines · sha256 51d26958a410…
#!/usr/bin/env -S fragletc --image ofthemachine/python3@sha256:a015b4f9f0e7c1648f8fcd4164f6a296dd3c9189404f88f333a9764d08d13522
#: d=Where an IP address is, from ip-api.com (no API key; its free tier is plain HTTP, so treat the answer as a hint, not a secret). Prints one line "City, Region, Country (CC) lat,lon tz" -- the City is the shape weather/current.py, weather/forecast.py and web/search.py take as their location. Pass ip= to look up a specific address; omitted, the caller's own public address is located.
#: when=Use when a workflow must find out where it is running (which city) before fetching local weather or news, or when the user asks where an IP address is.
#: network=required
#: stdin=none
#: param=ip:d=IPv4 or IPv6 address to locate; omitted, the caller's own public address
import json
import os
import sys
import urllib.parse
import urllib.request

ip = os.environ.get("IP", "").strip()
fields = "status,message,city,regionName,country,countryCode,lat,lon,timezone,query"
url = f"http://ip-api.com/json/{urllib.parse.quote(ip)}?fields={fields}"

try:
    with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "curl"}), timeout=15) as resp:
        data = json.load(resp)
except Exception as e:
    print(f"ip-location lookup failed: {e}", file=sys.stderr)
    sys.exit(1)

if data.get("status") != "success":
    print(f"ip-location lookup failed for {data.get('query') or ip or 'own address'}: {data.get('message', 'no reason given')}", file=sys.stderr)
    sys.exit(1)

print(f"{data['city']}, {data['regionName']}, {data['country']} ({data['countryCode']}) {data['lat']},{data['lon']} {data['timezone']}")