-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
66 lines (66 loc) · 2.85 KB
/
server.py
File metadata and controls
66 lines (66 loc) · 2.85 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
"""Main module to run Sandbox server"""
from os import geteuid, path, chdir
import subprocess
from shutil import copy2
from json import dumps, loads
#from uuid import uuid4
try:
from flask import Flask, request
except ImportError:
print "You have to install flask: pip install flask"
exit()
if not geteuid() == 0:
print "You must be root in order to run this application"
exit()
if not path.isdir("isolate"):
subprocess.call("git submodule update --init", shell=True)
if not path.isfile("isolate/isolate"):
chdir("isolate")
subprocess.call("make isolate", shell=True)
chdir("..")
if not path.isfile("/usr/local/etc/isolate"):
copy2("isolate/default.cf", "/usr/local/etc/isolate")
subprocess.call("isolate/isolate --cleanup", shell=True)
isolate_sandbox = subprocess.Popen("./isolate/isolate --init", shell=True, stdout=subprocess.PIPE).stdout.read().replace("\n", '')
app = Flask(__name__)
@app.route('/')
def hello_world():
"""Home page"""
return '<h1>Sandbox backend</h1>'
@app.route('/run', methods=['GET', 'POST'])
def esegui():
"""Run code with input and return output"""
global isolate_sandbox
if not request.method == "POST":
return """
<h1>Invalid request</h1>
<form method="POST" action="run">
<textarea name="code"></textarea><br>
<textarea name="input"></textarea><br>
Time: <input type="text" name="time"><br>
Mem: <input type="text" name="memory"><br>
<input type="submit">
"""
file_write = open(isolate_sandbox+"/box/run.cpp", 'w')
file_write.write(request.form["code"])
file_write.close()
file_write = open(isolate_sandbox+"/box/input.txt", 'w')
file_write.write(request.form["input"])
file_write.close()
compil=subprocess.Popen("cd "+isolate_sandbox+"/box&&g++ -std=c++0x run.cpp", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
compil.wait()
compOut = compil.stderr.read()
if not compOut=="":
return dumps({"compilation":False, "error":compOut})
time = float(request.form["time"])
memory = int(float(request.form["memory"])*1000)
res = subprocess.Popen("./isolate/isolate --run a.out -t "
"" + str(time) + " -w " + str(3*time) + " -m " + str(memory) + " <"
" "+isolate_sandbox+"/box/input.txt > "+isolate_sandbox+"/box/output"
".txt", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
res.wait()
exec_res = open(isolate_sandbox+"/box/output.txt", 'r').read()
subprocess.call("isolate/isolate --cleanup", shell=True)
isolate_sandbox = subprocess.Popen("./isolate/isolate --init", shell=True, stdout=subprocess.PIPE).stdout.read().replace("\n", '')
return dumps({"compilation":True, "output":exec_res, "exit_status":res.stderr.read()})
app.run(host='0.0.0.0', port=loads(open("config.json").read())["port"], debug=True)