Skip to content

Instantly share code, notes, and snippets.

@denysxftr
Created May 28, 2017 20:33
Show Gist options
  • Save denysxftr/474c630e669135a29c9b9024727b7130 to your computer and use it in GitHub Desktop.
Save denysxftr/474c630e669135a29c9b9024727b7130 to your computer and use it in GitHub Desktop.
from flask import Flask, abort, request
import json
import subprocess
import hashlib
import os
app = Flask(__name__)
@app.route("/submit", methods=["POST"])
def submit():
if not request.json: abort(400)
code = request.json["code"]
filename = hashlib.sha1(code.encode()).hexdigest() + ".py"
with open(os.path.join("tmp", filename), 'w') as f:
f.write(code)
result = subprocess.run("docker run -it --rm --name running-script -v \"$PWD\"/tmp:/test python:slim python /test/{}".format(filename), stdout=subprocess.PIPE, shell=True)
return json.dumps({"result": result.stdout.decode("utf-8").strip()})
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment