-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_code.py
More file actions
83 lines (67 loc) · 2.88 KB
/
run_code.py
File metadata and controls
83 lines (67 loc) · 2.88 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
82
83
import os
import sys
import traceback
import json
import subprocess
import io
from flask import Flask, request, render_template, session, redirect
from workdir import WorkDir
def SetFunctions(app):
@app.route("/getFileList",methods=("POST",))
def getFileList():
'''读取用户工作文件夹,获取文件列表,通过json方式返回'''
user_name = session["userName"]
with WorkDir(user_name):
data=[{"fileName":fileName} for fileName in os.listdir(".\\")]
jsonData = json.dumps(data, sort_keys=True,
indent=4, separators=(',', ': '))
# print(jsonData)
return jsonData
@app.route("/getFile",methods=("POST",))
def getFile():
'''获取特定文件的内容'''
user_name = session["userName"]
file_name = request.form["fileName"]
with WorkDir(user_name):
with open(file_name) as f:
content = f.read()
return content
@app.route("/deleteFile",methods=("POST",))
def deleteFile():
'''执行删除文件命令'''
user_name = session["userName"]
file_name = request.form["fileName"]
with WorkDir(user_name):
os.remove(file_name)
return ""
@app.route("/saveCode",methods=("POST",))
def saveCode():
'''保存文件:从request读入code和fileName,将code中的代码存进fileName文件里'''
user_name = session["userName"]
code=request.form["code"]
file_name = request.form["fileName"]
overWrite = request.form["overWrite"]
with WorkDir(user_name):
if overWrite=="false" and os.path.exists(file_name): # 判断该文件名是否已被使用
return "file exists"
with open(file_name,"w") as f:
f.write(code)
return "success"
@app.route("/runCode",methods=("POST",))
def runCode():
'''运行代码fileName,并将输出和错误信息一并返回'''
user_name = session["userName"]
file_name=request.form["fileName"]
with WorkDir(user_name):
proc = subprocess.Popen(f"python {file_name}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=-1)
proc.wait()
# 设置流,用于获取程序输出
stream_stdout = io.TextIOWrapper(proc.stdout, encoding='utf-8')
stream_stderr = io.TextIOWrapper(proc.stderr, encoding='utf-8')
str_stdout = str(stream_stdout.read())
str_stderr = str(stream_stderr.read())
# 防止输出traceback时,服务器内部组织泄露,把所有工作目录替换为'.'
str_stderr = str_stderr.replace(os.getcwd(),".")
#print("stdout: " + str_stdout)
#print("stderr: " + str_stderr)
return str_stdout + str_stderr