-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpServer.py
119 lines (115 loc) · 4.59 KB
/
httpServer.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import http
import http.server
from http import HTTPStatus
import os
import sys
import urllib
import configHandler
import http.client
import constants
def getjs(file):
path = sys.path[0]
os.chdir(path)
with open(file, 'r') as jsfile:
return jsfile.read()
def virtKeyboard(keymap: list[dict[str,list]],columns):
keystr = ""
for y in range(keymap.__len__()):
x=0
line=keymap[y]
for key in line.keys():
addBackslash=key=="'" or key=="\\"
backslash="\\"
x=x+line[key][3] if line[key].__len__()>=4 else x
keystr=keystr+f"""<button class='key' id="keyboard_{key}" type="button" onclick="pressKey('{key if not addBackslash else backslash+key}',-1)" style="
width:{line[key][1]/columns*100}%; height:{line[key][2]*40}px;
position:absolute; left:{x/columns*100}%; top:{y*40}px;"> {line[key][0]} </button>"""
x=x+line[key][1]
return keystr
def getImages():
section=""
files=os.listdir("usbimages")
for file in files:
section=section+"<option value=\""+file+"\">"+file+"</option>"
return section
class ServerCore2(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, directory=None, **kwargs):
super().__init__(*args, **kwargs, directory = 'web')
def do_GET(self):
"""Serve a GET request."""
try:
req = self.send_head()
if req:
f,ctype=req
try:
if ctype=="text/html":
__DICT={
"wsport":configHandler.config["server"]["wsport"],
"streamurl":configHandler.config["stream"]["stream_url"],
"keyboard1":virtKeyboard(constants.KEYBOARD.KEYS1,14),
"keyboard2":virtKeyboard(constants.KEYBOARD.KEYS2,3),
"keyboard3":virtKeyboard(constants.KEYBOARD.KEYS3,4),
"images":getImages()
}
pagestr=f.read().decode()
self.wfile.write(pagestr.format(**__DICT).encode())
else:
self.copyfile(f, self.wfile)
finally:
f.close()
except:
pass
def send_head(self):
path = self.translate_path(self.path)
f = None
if os.path.isdir(path):
parts = urllib.parse.urlsplit(self.path)
if not parts.path.endswith('/'):
# redirect browser - doing basically what apache does
self.send_response(HTTPStatus.MOVED_PERMANENTLY)
new_parts = (parts[0], parts[1], parts[2] + '/',
parts[3], parts[4])
new_url = urllib.parse.urlunsplit(new_parts)
self.send_header("Location", new_url)
self.send_header("Content-Length", "0")
self.end_headers()
return None
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
self.send_response(HTTPStatus.NOT_FOUND)
return None
ctype = self.guess_type(path)
# check for trailing "/" which should return 404. See Issue17324
# The test for this was added in test_httpserver.py
# However, some OS platforms accept a trailingSlash as a filename
# See discussion on python-dev and Issue34711 regarding
# parseing and rejection of filenames with a trailing slash
if path.endswith("/"):
self.send_error(HTTPStatus.NOT_FOUND, "File not found")
return None
try:
f = open(path, 'rb')
except OSError:
self.send_error(HTTPStatus.NOT_FOUND, "File not found")
return None
try:
fs = os.fstat(f.fileno())
self.send_response(HTTPStatus.OK)
self.send_header("Content-type", ctype)
# self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified",
self.date_time_string(fs.st_mtime))
self.end_headers()
return f,ctype
except:
f.close()
raise
class Server:
def __init__(self,config:configHandler.configparser.SectionProxy):
self.server = http.server.HTTPServer((config['address'],int(config["port"])),ServerCore2)
def serve_forever(self):
self.server.serve_forever()