diff --git a/ts3eventscripts.py b/ts3eventscripts.py index 7e43f86..c686c04 100755 --- a/ts3eventscripts.py +++ b/ts3eventscripts.py @@ -18,7 +18,7 @@ instances[key].daemon = True instances[key].start() # start webinterface -t = Thread(target=ts3http, args=(instances)) +t = Thread(target=ts3http, kwargs={'inst':instances}) t.daemon = True t.start() diff --git a/ts3http.py b/ts3http.py index db322e3..579e901 100644 --- a/ts3http.py +++ b/ts3http.py @@ -5,15 +5,19 @@ """ Need: sudo pip3 install jinja2 """ +instances = None class ts3http(): - def __init__(self, instances): - print (instances) + def __init__(self, inst): + global instances + instances = inst serv = HTTPServer(('', 8080), MyRequestHandler) serv.serve_forever() class MyRequestHandler(CGIHTTPRequestHandler): def do_GET(self): + global instances + print(instances) try: path = './wwwroot/' + self.path.split('?')[0] if self.path == "/": @@ -25,38 +29,44 @@ def do_GET(self): self.wfile.write(bytes(tpl.render(), 'UTF-8')) f.close() return - elif '.html' in path: + elif self.path.split('/')[1] in instances: self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() - f = open(path) + f = open('./wwwroot/instance.html') tpl = Template(f.read()) - self.wfile.write(bytes(tpl.render(), 'UTF-8')) + self.wfile.write(bytes(tpl.render(instance=instances[self.path.split('/')[1]]), 'UTF-8')) + f.close() + return + elif '/assets/' in self.path: + fileName, fileExtension = os.path.splitext(path) + f = open(path, 'rb') + if fileExtension == '.png': + contentType = 'image/png' + elif fileExtension == '.jpg' or fileExtension == '.jpeg': + contentType = 'image/jpeg' + elif fileExtension == '.css': + contentType = 'text/css' + else: + contentType = 'text/html' + self.send_response(200) + self.send_header('Content-type', contentType) + self.end_headers() + self.wfile.write(f.read()) f.close() return else: - if '/assets/' in self.path: - fileName, fileExtension = os.path.splitext(path) - f = open(path, 'rb') - if fileExtension == '.png': - contentType = 'image/png' - elif fileExtension == '.jpg' or fileExtension == '.jpeg': - contentType = 'image/jpeg' - elif fileExtension == '.css': - contentType = 'text/css' - else: - contentType = 'text/html' - self.send_response(200) - self.send_header('Content-type', contentType) - self.end_headers() - self.wfile.write(f.read()) - f.close() - return + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + f = open('./wwwroot/templates/404.html') + tpl = Template(f.read()) + self.wfile.write(bytes(tpl.render(), 'UTF-8')) + f.close() except IOError: - # self.send_error(404, 'file not found: %s' % self.path) self.send_response(200) self.send_header("Content-type", "text/html") - self.end_headers() + # self.end_headers() f = open('./wwwroot/templates/404.html') tpl = Template(f.read()) self.wfile.write(bytes(tpl.render(), 'UTF-8'))