46 lines
1.6 KiB
Plaintext
46 lines
1.6 KiB
Plaintext
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
import os
|
|
from pathlib import Path
|
|
|
|
HTTP_ADDR = os.getenv('env_http_addr', '0.0.0.0')
|
|
HTTP_PORT = int(os.getenv('env_http_port', '8080'))
|
|
LOGFILE_PATH = os.getenv('env_logfile_path', 'logfile.json')
|
|
json_string = '{"receiver-stats":null}'
|
|
|
|
class StatsServer(BaseHTTPRequestHandler):
|
|
def _set_headers(self):
|
|
self.send_response(200)
|
|
self.send_header('Content-type', 'application/json')
|
|
self.send_header('Access-Control-Allow-Origin', '*')
|
|
self.end_headers()
|
|
def do_HEAD(self):
|
|
self._set_headers()
|
|
def do_GET(self):
|
|
self._set_headers()
|
|
global json_string
|
|
if Path.exists(Path(LOGFILE_PATH)):
|
|
with open(LOGFILE_PATH, 'r') as f:
|
|
json_string = f.read()
|
|
byte_json_string_utf8 = json_string.encode('utf-8')
|
|
print(byte_json_string_utf8)
|
|
self.wfile.write(byte_json_string_utf8)
|
|
|
|
def runhttpserver(server_class=HTTPServer, handler_class=StatsServer, port=HTTP_PORT):
|
|
print(f'Starting httpd http://{HTTP_ADDR}:{HTTP_PORT}...\n')
|
|
global json_string
|
|
print(json_string)
|
|
server_address = (HTTP_ADDR, port)
|
|
httpd = server_class(server_address, handler_class)
|
|
try:
|
|
httpd.serve_forever()
|
|
except KeyboardInterrupt:
|
|
pass
|
|
httpd.server_close()
|
|
print(f'... Stopped Httpd Server \n')
|
|
|
|
if __name__ == "__main__":
|
|
print(Path("banner.txt").read_text())
|
|
if Path.exists(Path(LOGFILE_PATH)):
|
|
with open(LOGFILE_PATH, 'r') as f:
|
|
json_string = f.read()
|
|
runhttpserver() |