-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.py
34 lines (26 loc) · 1.05 KB
/
api.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
import plistlib
import json
from urllib.request import urlopen, Request
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
class APIHandler(BaseHTTPRequestHandler):
def do_GET(self):
host = self.path[1:]
if host == 'health':
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({"Status": "OK"}).encode())
else:
status = self.get_status(host)
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({"Status": status}).encode())
def get_status(self, host):
httprequest = Request(f"http://{host}:7000/info")
with urlopen(httprequest, timeout=60) as response:
content = response.read()
data = plistlib.loads(content)
return data["statusFlags"] > 2000
httpd = ThreadingHTTPServer(("0.0.0.0", 8000), APIHandler)
httpd.serve_forever()