Created
May 28, 2017 20:33
-
-
Save denysxftr/474c630e669135a29c9b9024727b7130 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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