-
Notifications
You must be signed in to change notification settings - Fork 2
/
master.py
43 lines (36 loc) · 1.09 KB
/
master.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from flask import Flask
from flask import render_template
import requests
import json
from datetime import datetime
import pytz
from config import CONFIG
tz = pytz.timezone("Asia/Taipei")
app = Flask(__name__)
app.config["TEMPLATES_AUTO_RELOAD"] = True
SITE_TITLE = CONFIG.get("site_title", "Server status")
TOP_MESSAGE = CONFIG.get("top_message", "Hello world")
if CONFIG.get("server_ips") is None:
raise ValueError()
SERVER_IPS = CONFIG.get("server_ips")
@app.route('/')
def server():
servers = list()
now = datetime.now(tz=tz).strftime("%Y-%m-%d %T")
for ip in SERVER_IPS:
resp = requests.get(f"http://{ip}:23333")
if resp.status_code != 200:
data = {
"ip": ip,
"active": False
}
else:
data = json.loads(resp.text)
data["ip"] = ip
data["active"] = True
servers.append(data)
context = {"title": SITE_TITLE,
"top_message": TOP_MESSAGE,
"now": now,
"servers": servers}
return render_template("index.html", **context)