-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
81 lines (67 loc) · 2.51 KB
/
server.py
File metadata and controls
81 lines (67 loc) · 2.51 KB
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
import os
import json
from os import walk
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse
from utils import TraceFile
from utils.lru_cache import CacheLRU
PORT = 8080
HOST = '127.0.0.1'
class ServerHandler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
def do_GET(self):
# Get the algorithm ID
query = urlparse(self.path).query
id = ''
if (len(query)):
query_components = dict(qc.split("=") for qc in query.split("&"))
id = query_components.get('id', '')
# Trace all algorithms
if '/trace_all' in self.path:
self._set_headers()
# 1: Get the file names
files_to_trace = filenames = next(
walk("algorithms/"), (None, None, []))[2]
# 2: Trace the files concurrently
trace_with_threads = TraceFile(files_to_trace)
result = trace_with_threads.trace_all()
data = json.dumps({"results": result})
self.wfile.write(data.encode())
# Trace one algorithm
elif '/trace' in self.path:
if len(id) > 0:
os.system('python {}.py'.format(id))
print(id)
self.send_response(200)
self.send_response(404)
# Return the traces of one algorithm
elif '/algorithm' in self.path:
if len(id) > 0:
self._set_headers()
data = lru_cache.get(id)
if not data:
with open('output/{}.json'.format(id)) as data_file:
data = data_file.read()
print('read from file')
lru_cache.set(id, data)
self.wfile.write(data.encode())
# Return the source code of one algorithms
elif '/file' in self.path:
if len(id) > 0:
self._set_headers()
with open('{}.py'.format(id)) as data_file:
data = data_file.read()
data = json.dumps({"file": data})
self.wfile.write(data.encode())
else:
self.send_response(404)
self.end_headers()
if __name__ == "__main__":
cache_size = 2
lru_cache = CacheLRU(cache_size)
webServer = HTTPServer((HOST, PORT), ServerHandler)
print("Server started http://%s:%s" % (HOST, PORT))
webServer.serve_forever()