forked from isaacg1/pyth
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·74 lines (56 loc) · 2.5 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
74
#!venv/bin/python
from flask import Flask, render_template, request, Response
import os
import time
import subprocess
app = Flask(__name__, template_folder='.', static_folder='.')
@app.route('/')
def root():
time_in_secs = os.path.getmtime('pyth.py')
time_in_python = time.gmtime(time_in_secs)
formatted_time = time.strftime("%d %b %Y", time_in_python)
return render_template('index.html',
formatted_time=formatted_time,
code=request.args.get('code', ''),
input=request.args.get('input', ''),
debug=int(request.args.get('debug', 0)),
test_suite=int(request.args.get('test_suite', 0)),
test_suite_input=request.args.get('test_suite_input', ''),
input_size=int(request.args.get('input_size', 1)))
def run_code(code_message, input_message, debug_on):
resp = ''
input_message += '\n'
pyth_code = '\n'.join(code_message.split("\r\n"))
pyth_process = \
subprocess.Popen(['/usr/bin/env',
'python3',
'pyth.py',
'-csd' if debug_on else '-cs',
pyth_code],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
output, errors = \
pyth_process.communicate(input=bytearray(input_message, 'utf-8'))
if code_message:
resp += output.decode() + (errors if errors else '')
return resp
@app.route('/submit', methods=['POST'])
def submit():
code_message = request.form.get('code', '')
input_message = request.form.get('input', '')
debug_on = int(request.form.get('debug'), 0)
return Response(run_code(code_message, input_message, debug_on))
@app.route('/submit_test_suite', methods=['POST'])
def submit_test_suite():
code_message = request.form.get('code', '')
input_size = int(request.form.get('input_size', '1'), 0)
inputs = ["\n".join(i) for i in zip(*[iter(request.form.get('input', '').split("\n"))] * input_size)]
debug_on = int(request.form.get('debug'), 0)
return Response("\n".join([run_code(code_message, inputs[0], debug_on)] +
[run_code(code_message, i, False) for i in inputs[1:]]) if inputs else "")
@app.route('/<path>')
def other(path):
return app.send_static_file(path)
if __name__ == '__main__':
app.run(debug=True)