forked from hcientist/OnlinePythonTutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottle_server.py
65 lines (51 loc) · 1.81 KB
/
bottle_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
# Lightweight OPT server that works on both Python 2 and 3
# NOTE that this is meant only for testing and not deployment, since
# there is no sandboxing
# to invoke, run 'python bottle_server.py'
# and visit http://localhost:8080/index.html
#
# external dependencies: bottle
#
# easy_install pip
# pip install bottle
from bottle import route, get, request, run, template, static_file
try:
import StringIO # NB: don't use cStringIO since it doesn't support unicode!!!
except:
import io as StringIO # py3
import json
import pg_logger
@route('/web_exec_<name:re:.+>.py')
@route('/LIVE_exec_<name:re:.+>.py')
@route('/viz_interaction.py')
@route('/syntax_err_survey.py')
@route('/runtime_err_survey.py')
@route('/eureka_survey.py')
@route('/error_log.py')
def dummy_ok(name=None):
return 'OK'
@route('/<filepath:path>')
def index(filepath):
return static_file(filepath, root='.')
# Note that this will run either Python 2 or 3, depending on which
# version of Python you used to start the server, REGARDLESS of which
# route was taken:
@route('/web_exec_py2.py')
@route('/web_exec_py3.py')
@route('/LIVE_exec_py2.py')
@route('/LIVE_exec_py3.py')
def get_py_exec():
out_s = StringIO.StringIO()
def json_finalizer(input_code, output_trace):
ret = dict(code=input_code, trace=output_trace)
json_output = json.dumps(ret, indent=None)
out_s.write(json_output)
options = json.loads(request.query.options_json)
pg_logger.exec_script_str_local(request.query.user_script,
request.query.raw_input_json,
options['cumulative_mode'],
options['heap_primitives'],
json_finalizer)
return out_s.getvalue()
if __name__ == "__main__":
run(host='localhost', port=8003, reloader=True)