11# Lightweight OPT server that works on both Python 2 and 3
22
3+ # NOTE that this is meant only for testing and not deployment, since
4+ # there is no sandboxing
5+
36# to invoke, run 'python bottle_server.py'
47# and visit http://localhost:8080/index.html
58#
811# easy_install pip
912# pip install bottle
1013
11- # From an OPT user: A couple notes: to get bottle_server.py running,
12- # I had to replace cStringIO with io and urllib2 with urllib, for
13- # compatibility from 2.x to 3.x Ii was running from /v3/).
14-
1514from bottle import route , get , request , run , template , static_file
16- import StringIO # NB: don't use cStringIO since it doesn't support unicode!!!
15+ try :
16+ import StringIO # NB: don't use cStringIO since it doesn't support unicode!!!
17+ except :
18+ import io as StringIO # py3
1719import json
1820import pg_logger
19- import urllib
20- import urllib2
2121
2222# dummy routes for testing only
2323@route ('/web_exec_<name:re:.+>.py' )
@@ -28,14 +28,17 @@ def web_exec(name):
2828def live_exec (name ):
2929 return 'OK'
3030
31+ @route ('/viz_interaction.py' )
32+ def viz_interaction ():
33+ return 'OK'
3134
3235@route ('/<filepath:path>' )
3336def index (filepath ):
34- # special-case for testing name_lookup.py ...
35- if 'name_lookup.py' in filepath :
36- return json .dumps (dict (name = 'TEST NAME' , email = 'TEST EMAIL' ))
3737 return static_file (filepath , root = '.' )
3838
39+
40+ # Note that this will run either Python 2 or 3, depending on which
41+ # version of Python you used to start the server.
3942@get ('/exec' )
4043def get_exec ():
4144 out_s = StringIO .StringIO ()
@@ -55,63 +58,5 @@ def json_finalizer(input_code, output_trace):
5558
5659 return out_s .getvalue ()
5760
58-
59- @get ('/load_matrix_problem.py' )
60- def load_matrix_problem ():
61- prob_name = request .query .problem_name
62- assert type (prob_name ) in (str , unicode )
63-
64- # whitelist
65- assert prob_name in ('python_comprehension-1' ,)
66-
67- fn = 'matrix-demo/' + prob_name + '.py'
68- f = open (fn )
69- cod = f .read ()
70- f .close ()
71-
72- import doctest
73- import sys
74- p = doctest .DocTestParser ()
75- examples = p .get_examples (cod )
76- if len (examples ):
77- first_ex = examples [0 ]
78- #print >> sys.stderr, 'Source:', `first_ex.source`
79- testCod = 'result = ' + first_ex .source
80-
81- return json .dumps (dict (code = cod , test = testCod ))
82-
83-
84- @get ('/submit_matrix_problem.py' )
85- def submit_matrix_problem ():
86- user_code = request .query .submitted_code
87- prob_name = request .query .problem_name
88- assert type (prob_name ) in (str , unicode )
89-
90- # whitelist
91- assert prob_name in ('python_comprehension-1' ,)
92-
93- test_fn = 'matrix-demo/' + prob_name + '.test.py'
94- test_cod = open (test_fn ).read ()
95-
96- # concatenate!
97- script = test_cod + '\n ' + user_code + \
98- '''
99- import doctest
100- (n_fail, n_tests) = doctest.testmod(verbose=False)
101- if n_fail == 0:
102- print("All %d tests passed!" % n_tests)
103- '''
104-
105- url = 'http://ec2-107-20-94-197.compute-1.amazonaws.com/cgi-bin/run_code.py'
106- values = {'user_script' : script }
107-
108- data = urllib .urlencode (values )
109- req = urllib2 .Request (url , data )
110- response = urllib2 .urlopen (req )
111- the_page = response .read ()
112- return the_page
113-
114-
11561if __name__ == "__main__" :
116- #run(host='localhost', port=8080, reloader=True)
117- run (host = '0.0.0.0' , port = 8003 , reloader = True ) # make it externally visible - DANGER this is very insecure since there's no sandboxing!
62+ run (host = 'localhost' , port = 8003 , reloader = True )
0 commit comments