Files
myip/app.py
2025-11-26 09:48:58 +00:00

54 lines
1.6 KiB
Python

from flask import Flask, request, render_template_string
import requests
app = Flask(__name__)
HTML_TEMPLATE = """<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<title>Il tuo IP pubblico</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Il tuo IP pubblico è: {{ ip }}</h2>
{% if ripe %}
<h3>Informazioni RIPE</h3>
<p>Provider: {{ ripe.holder }}</p>
<p>Prefisso: {{ ripe.prefix }}</p>
<p>ASN: {{ ripe.asns|join(', ') }}</p>
{% endif %}
</body>
</html>
"""
def get_client_ip():
xff = request.headers.get("X-Forwarded-For", "")
if xff:
return xff.split(",")[0].strip()
return request.remote_addr or "Sconosciuto"
def fetch_ripe_info(ip: str):
try:
ni = requests.get("https://stat.ripe.net/data/network-info/data.json",
params={"resource": ip}, timeout=2).json().get("data", {})
prefix = ni.get("prefix")
asns = ni.get("asns") or []
holder=None
if asns:
ao = requests.get("https://stat.ripe.net/data/as-overview/data.json",
params={"resource": asns[0]}, timeout=2).json().get("data", {})
holder = ao.get("holder")
return {"prefix": prefix, "asns":[f"AS{a}" for a in asns], "holder": holder}
except:
return None
@app.route("/")
def index():
ip = get_client_ip()
ripe = fetch_ripe_info(ip) if ip!="Sconosciuto" else None
return render_template_string(HTML_TEMPLATE, ip=ip, ripe=ripe)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)