-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
74 lines (64 loc) · 2.39 KB
/
server.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
from http.server import HTTPServer, BaseHTTPRequestHandler
import argparse
import pathlib
import subprocess
import json
server = None
path_to_emu = None
def compile_asm(asm):
temp_file = open("temp-asm.s", mode="w")
temp_file.write(asm)
temp_file.flush()
command = ['nasm', '-f', 'bin', '-o', '/dev/stdout', 'temp-asm.s']
command2 = [path_to_emu, '--stdin']
result = subprocess.run(command, capture_output=True)
if result.returncode != 0 or len(result.stderr)>0:
temp_file.close()
return False, result.stderr
temp_file.close()
nasm_proc = subprocess.Popen(command, stdout=subprocess.PIPE)
emu_proc = subprocess.Popen(command2, stdin=nasm_proc.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = emu_proc.communicate()
pathlib.Path.unlink("temp-asm.s")
if len(stderr) > 0:
return False, stderr
return True, stdout
class Foo(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
file = open("index.html")
self.wfile.write(file.read().encode())
def do_UPDATE(self):
self.send_response(200)
self.end_headers()
server.server_close()
print("Closing server")
exit(0)
def do_POST(self):
body = self.rfile.read(int(self.headers["Content-Length"])).decode()
success, res = compile_asm(body)
if not success:
self.send_response(404)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps({"error":res.decode()}).encode())
else:
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(res)
def main():
global path_to_emu
global server
parser = argparse.ArgumentParser(usage="python3 server.py -f path/to/emu")
parser.add_argument("-f", "--file", required=True, help="path to emu required")
parser.add_argument("-p", "--port", help="port", default="8023")
args = parser.parse_args()
path_to_emu = args.file
print(f"serving on port localhost:{args.port}")
server = HTTPServer(("localhost", int(args.port)), Foo)
server.serve_forever()
main()