完成一个测试服务器
parent
8db6ac26b9
commit
dc3fc432b5
@ -0,0 +1,19 @@
|
||||
import requests
|
||||
URL = "http://192.168.230.132:8011"
|
||||
|
||||
code = """
|
||||
import time
|
||||
|
||||
print("hello world")
|
||||
cnt = 0
|
||||
while True:
|
||||
print(cnt)
|
||||
cnt += 1
|
||||
time.sleep(0.1)
|
||||
|
||||
"""
|
||||
|
||||
resp = requests.post(URL, json={"code": code})
|
||||
print(resp.json()["msg"])
|
||||
|
||||
|
||||
@ -0,0 +1,48 @@
|
||||
from flask import Flask, request
|
||||
import random
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
app = Flask(__name__)
|
||||
ROCKER_PATH = "/home/yanguangshaonian/rocker/target/debug/rocker"
|
||||
|
||||
@app.route("/", methods=["POST"])
|
||||
def hello_world():
|
||||
_id = str(random.randint(8000_0000, 9000_0000))
|
||||
user_path = f"/tmp/{_id}"
|
||||
os.mkdir(user_path)
|
||||
code = request.json.get("code", "")
|
||||
with open(f"{user_path}/main.py", "w") as f:
|
||||
f.write(code)
|
||||
|
||||
try:
|
||||
_ = subprocess.run(["sudo", ROCKER_PATH,
|
||||
"--id", _id,
|
||||
"--image", "ubuntu_pypy_numpy_pandas_user",
|
||||
"--run", "python -u /tmp/main.py",
|
||||
"--env", "/home/yanguangshaonian/rocker/pypy.env",
|
||||
"--volume", f"{user_path}:/tmp",
|
||||
"--log"])
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
# 读取文件
|
||||
out = ""
|
||||
try:
|
||||
log_path = f"/home/rocker/containers/{_id}/upper/logs/log"
|
||||
print(f"读取: {log_path}")
|
||||
with open(log_path) as f:
|
||||
out = f.read()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
subprocess.run(["sudo", ROCKER_PATH,
|
||||
"--rm", _id])
|
||||
subprocess.run(["sudo", "rm", "-rf", user_path])
|
||||
return {"msg": out, code: code, "id": _id}
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host='0.0.0.0', port=8011)
|
||||
Loading…
Reference in New Issue