Files
sipcalc-webui/app.py
T
2026-10-22 21:17:31 -04:00

51 lines
1.7 KiB
Python

import subprocess
from flask import Flask, render_template_string, request
app = Flask(__name__)
HTML_TEMPLATE = """
<!DOCTYPE html>
<html>
<head>
<title>IPv6 Subnet Calculator</title>
<style>
body { font-family: monospace; background: #1e1e2e; color: #cdd6f4; padding: 20px; }
input, button { padding: 10px; font-size: 16px; border-radius: 4px; border: none; }
input { width: 350px; background: #313244; color: #fff; }
button { background: #89b4fa; color: #11111b; cursor: pointer; font-weight: bold; }
pre { background: #181825; padding: 15px; border-radius: 6px; border: 1px solid #45475a; }
</style>
</head>
<body>
<h2>IPv6 Subnet Calculator (sipcalc Wrapper)</h2>
<form method="POST">
<input type="text" name="ipv6" placeholder="2001:db8:85a3::8a2e:370:7334/64" value="{{ query }}">
<button type="submit">Calculate</button>
</form>
{% if output %}
<h3>Results:</h3>
<pre>{{ output }}</pre>
{% endif %}
</body>
</html>
"""
@app.route("/", methods=["GET", "POST"])
def index():
output = ""
query = ""
if request.method == "POST":
query = request.form.get("ipv6", "").strip()
if query:
try:
# Run sipcalc CLI command securely
result = subprocess.run(
["sipcalc", query], capture_output=True, text=True, timeout=5
)
output = result.stdout if result.returncode == 0 else result.stderr
except Exception as e:
output = str(e)
return render_template_string(HTML_TEMPLATE, query=query, output=output)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)