forked from Schweigi/assembler-simulator
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSimserver.py
executable file
·71 lines (68 loc) · 2.53 KB
/
Simserver.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
#!/usr/bin/env python
import BaseHTTPServer
import urlparse
import os
import unittest
import json
from cucu.cucu import CucuVM
class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(self):
payload = json.loads(self.rfile.read(int(self.headers['content-length'])))
source = payload["source"].split('\n')
real_source = ''
for line in source:
if not line.startswith(';'):
real_source += line + '\n'
c = CucuVM(real_source)
parsed_path = urlparse.urlparse(self.path)
message_parts = [
'CLIENT VALUES:',
'client_address=%s (%s)' % (self.client_address,
self.address_string()),
'command=%s' % self.command,
'path=%s' % self.path,
'real path=%s' % parsed_path.path,
'query=%s' % parsed_path.query,
'request_version=%s' % self.request_version,
'',
'SERVER VALUES:',
'server_version=%s' % self.server_version,
'sys_version=%s' % self.sys_version,
'protocol_version=%s' % self.protocol_version,
'',
'HEADERS RECEIVED:',
]
for name, value in sorted(self.headers.items()):
message_parts.append('%s=%s' % (name, value.rstrip()))
message_parts.append('')
message = '\n'.join(message_parts)
self.send_response(200)
self.end_headers()
self.wfile.write(c.code)
def do_GET(self):
parsed_path = urlparse.urlparse(self.path)
pa = self.path;
self.send_response(200)
self.end_headers()
if(pa[0:17] == "/examples/scandir"):
info = os.getcwd();
arr = sorted(os.listdir(info+"/examples"));
results = []
for fn in arr:
if not fn.endswith('.txt'):
continue
f = open('examples/' + fn, 'r')
line = f.readlines()[0].strip()
if 'HIDE' in line:
continue
results.append('"%s|%s"' % (fn, line[2:]))
text = '[%s]' % ','.join(results)
self.wfile.write(text);
else:
if pa == "/":
pa = "/index.html"
input = open(pa[1:],"r");
self.wfile.write(input.read());
input.close()
server = BaseHTTPServer.HTTPServer(('0.0.0.0',8082), WebRequestHandler)
server.serve_forever()