58 lines
1.4 KiB
Python
58 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import re
|
|
import requests
|
|
|
|
BASE_URL = "http://geo-block.krk.itg.demo-ht.iadm"
|
|
ENDPOINT = "/api/generate/raw"
|
|
|
|
payload_json = """{
|
|
"countries": [
|
|
"PL"
|
|
],
|
|
"aggregate": true,
|
|
"use_cache": true,
|
|
"app_type": "raw-cidr_json",
|
|
"as_js": false
|
|
}"""
|
|
payload = json.loads(payload_json)
|
|
|
|
resp = requests.post(BASE_URL + ENDPOINT, json=payload, timeout=120)
|
|
|
|
print("Status:", resp.status_code)
|
|
print("X-From-Cache:", resp.headers.get("X-From-Cache"))
|
|
print("X-Cache-Type:", resp.headers.get("X-Cache-Type"))
|
|
print("X-Generated-At:", resp.headers.get("X-Generated-At"))
|
|
|
|
ct = (resp.headers.get("Content-Type") or "").lower()
|
|
|
|
if resp.status_code >= 400:
|
|
try:
|
|
print(json.dumps(resp.json(), indent=2))
|
|
except Exception:
|
|
print(resp.text)
|
|
raise SystemExit(1)
|
|
|
|
if "application/json" in ct:
|
|
print(json.dumps(resp.json(), indent=2))
|
|
else:
|
|
filename = "output"
|
|
cd = resp.headers.get("Content-Disposition") or ""
|
|
m = re.search(r'filename="?([^"]+)"?', cd)
|
|
if m:
|
|
filename = m.group(1)
|
|
else:
|
|
if "text/csv" in ct:
|
|
filename += ".csv"
|
|
elif "javascript" in ct:
|
|
filename += ".js"
|
|
elif "text/plain" in ct:
|
|
filename += ".txt"
|
|
else:
|
|
filename += ".bin"
|
|
|
|
with open(filename, "wb") as f:
|
|
f.write(resp.content)
|
|
|
|
print("Saved to:", filename)
|