diff options
Diffstat (limited to 'snekbox.py')
-rw-r--r-- | snekbox.py | 58 |
1 files changed, 21 insertions, 37 deletions
@@ -1,10 +1,8 @@ -import json -import multiprocessing import os import subprocess import sys -from rmq import Rmq +from flask import Flask, render_template, request, jsonify class Snekbox: @@ -98,46 +96,32 @@ class Snekbox: return output - def execute(self, body): - """ - Handles execution of a raw JSON-formatted RMQ message, contained in ``body``. - The message metadata, including the Python code to be executed, is - extracted from the message body. The code is then executed in the - isolated environment, and the results of the execution published - to RMQ. Once published, the system exits, since the snekboxes - are created and disposed of per-execution. - """ +snekbox = Snekbox() - msg = body.decode('utf-8') - result = '' - snek_msg = json.loads(msg) - snekid = snek_msg['snekid'] - snekcode = snek_msg['message'].strip() +# Load app +app = Flask(__name__) +app.use_reloader = False - result = self.python3(snekcode) +# Logging +log = app.logger - rmq.publish(result, - queue=snekid, - routingkey=snekid, - exchange=snekid) - exit(0) - def message_handler(self, ch, method, properties, body, thread_ws=None): - """Spawns a daemon process that handles RMQ messages.""" [email protected]('/') +def index(): + return render_template('index.html') - p = multiprocessing.Process(target=self.execute, args=(body,)) - p.daemon = True - p.start() - ch.basic_ack(delivery_tag=method.delivery_tag) [email protected]('/result', methods=["POST", "GET"]) +def result(): + if request.method == "POST": + code = request.form["Code"] + output = snekbox.python3(code) + return render_template('result.html', code=code, result=output) -if __name__ == '__main__': - try: - rmq = Rmq() - snkbx = Snekbox() - rmq.consume(callback=snkbx.message_handler) - except KeyboardInterrupt: - print('Exited') - exit(0) [email protected]('/input', methods=["POST"]) +def code_input(): + body = request.get_json() + output = snekbox.python3(body["code"]) + return jsonify(input=body["code"], output=output) |