diff --git a/ts3eventscripts.py b/ts3eventscripts.py index fd54f27..7e43f86 100755 --- a/ts3eventscripts.py +++ b/ts3eventscripts.py @@ -8,15 +8,17 @@ config = configparser.ConfigParser() config.read('config.ini') +instances = {} # start ts3 instances for key in config: - if key.startswith('instance'): - ts3 = ts3base(config[key]) - ts3.daemon = True - ts3.start() + if key != 'DEFAULT': + config[key]['id'] = key + instances[key] = ts3base(config[key]) + instances[key].daemon = True + instances[key].start() # start webinterface -t = Thread(target=ts3http, args=()) +t = Thread(target=ts3http, args=(instances)) t.daemon = True t.start() diff --git a/ts3http.py b/ts3http.py index c69d0e6..db322e3 100644 --- a/ts3http.py +++ b/ts3http.py @@ -1,32 +1,40 @@ import os from http.server import HTTPServer, CGIHTTPRequestHandler +from jinja2 import Template +""" +Need: sudo pip3 install jinja2 +""" class ts3http(): - def __init__(self): + def __init__(self, instances): + print (instances) serv = HTTPServer(('', 8080), MyRequestHandler) serv.serve_forever() class MyRequestHandler(CGIHTTPRequestHandler): def do_GET(self): - path = './wwwroot/' + self.path.split('?')[0] - if self.path == "/": - self.send_response(200) - self.send_header("Content-type", "text/html") - self.end_headers() - f = open('./wwwroot/index.html') - self.wfile.write(bytes(f.read(), 'UTF-8')) - f.close() - elif '.html' in path: - self.send_response(200) - self.send_header("Content-type", "text/html") - self.end_headers() - f = open(path) - self.wfile.write(bytes(f.read(), 'UTF-8')) - f.close() - return - else: - try: + try: + path = './wwwroot/' + self.path.split('?')[0] + if self.path == "/": + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + f = open('./wwwroot/index.html') + tpl = Template(f.read()) + self.wfile.write(bytes(tpl.render(), 'UTF-8')) + f.close() + return + elif '.html' in path: + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + f = open(path) + tpl = Template(f.read()) + self.wfile.write(bytes(tpl.render(), 'UTF-8')) + f.close() + return + else: if '/assets/' in self.path: fileName, fileExtension = os.path.splitext(path) f = open(path, 'rb') @@ -44,8 +52,15 @@ def do_GET(self): self.wfile.write(f.read()) f.close() return - except IOError: - self.send_error(404, 'file not found: %s' % self.path) + 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() + f = open('./wwwroot/templates/404.html') + tpl = Template(f.read()) + self.wfile.write(bytes(tpl.render(), 'UTF-8')) + f.close() def do_POST(self): self.do_GET()