-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathts3http.py
76 lines (72 loc) · 2.73 KB
/
ts3http.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
from http.server import HTTPServer, CGIHTTPRequestHandler
from jinja2 import Template
"""
Need: sudo pip3 install jinja2
"""
instances = None
class ts3http():
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 == "/":
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 self.path.split('/')[1] in instances:
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
f = open('./wwwroot/instance.html')
tpl = Template(f.read())
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:
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_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()