From 2657852ee3e97ee2dc233c932bd7c88bceec94b1 Mon Sep 17 00:00:00 2001 From: Scragly <29337040+scragly@users.noreply.github.com> Date: Sun, 20 Jan 2019 20:42:34 +1000 Subject: Remove RMQ, Add API POST request method. --- snekbox.py | 65 +++++++++++++++++++++++++++++--------------------------------- 1 file changed, 30 insertions(+), 35 deletions(-) (limited to 'snekbox.py') diff --git a/snekbox.py b/snekbox.py index ddde563..4e3e4fa 100644 --- a/snekbox.py +++ b/snekbox.py @@ -1,17 +1,14 @@ -import json -import multiprocessing import subprocess import os import sys -from rmq import Rmq +from flask import Flask, render_template, request, jsonify class Snekbox(object): def __init__(self, nsjail_binary='nsjail', python_binary=os.path.dirname(sys.executable)+os.sep+'python3.6'): - self.nsjail_binary = nsjail_binary self.python_binary = python_binary self.nsjail_workaround() @@ -83,34 +80,32 @@ class Snekbox(object): return output - def execute(self, body): - msg = body.decode('utf-8') - result = '' - snek_msg = json.loads(msg) - snekid = snek_msg['snekid'] - snekcode = snek_msg['message'].strip() - - result = self.python3(snekcode) - - rmq.publish(result, - queue=snekid, - routingkey=snekid, - exchange=snekid) - exit(0) - - def message_handler(self, ch, method, properties, body, thread_ws=None): - p = multiprocessing.Process(target=self.execute, args=(body,)) - p.daemon = True - p.start() - - ch.basic_ack(delivery_tag=method.delivery_tag) - - -if __name__ == '__main__': - try: - rmq = Rmq() - snkbx = Snekbox() - rmq.consume(callback=snkbx.message_handler) - except KeyboardInterrupt: - print('Exited') - exit(0) + +snekbox = Snekbox() + +# Load app +app = Flask(__name__) +app.use_reloader = False + +# Logging +log = app.logger + + +@app.route('/') +def index(): + return render_template('index.html') + + +@app.route('/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) + + +@app.route('/input', methods=["POST"]) +def code_input(): + body = request.get_json() + output = snekbox.python3(body["code"]) + return jsonify(input=body["code"], output=output) -- cgit v1.2.3