Skip to content

Commit

Permalink
ts3http has now the ability to get all instances back. Also included …
Browse files Browse the repository at this point in the history
…a little template engine for future use. Changed a try catch in ts3http to get all 404 errors.
  • Loading branch information
derkalle4 committed Jun 11, 2015
1 parent 10ff58f commit 83fc462
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
12 changes: 7 additions & 5 deletions ts3eventscripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
57 changes: 36 additions & 21 deletions ts3http.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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()

0 comments on commit 83fc462

Please sign in to comment.