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 From 3f679bf41341a2b2546ae667998bd5ea5d80e3fe Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 25 Mar 2019 11:52:41 -0700 Subject: Add docstrings to routes --- snekbox.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'snekbox.py') diff --git a/snekbox.py b/snekbox.py index 5946e12..bb04987 100644 --- a/snekbox.py +++ b/snekbox.py @@ -109,11 +109,15 @@ log = app.logger @app.route('/') def index(): + """Return a page with a form for inputting code to be executed.""" + return render_template('index.html') @app.route('/result', methods=["POST", "GET"]) def result(): + """Execute code and return a page displaying the results.""" + if request.method == "POST": code = request.form["Code"] output = snekbox.python3(code) @@ -122,6 +126,8 @@ def result(): @app.route('/input', methods=["POST"]) def code_input(): + """Execute code and return the results.""" + body = request.get_json() output = snekbox.python3(body["code"]) return jsonify(input=body["code"], output=output) -- cgit v1.2.3 From 6b5b430c7fd0ed0398ca8ddbb76ae71b8e3d005f Mon Sep 17 00:00:00 2001 From: MarkKoz Date: Mon, 25 Mar 2019 12:03:06 -0700 Subject: Fix import order --- snekbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'snekbox.py') diff --git a/snekbox.py b/snekbox.py index bb04987..65fc4b3 100644 --- a/snekbox.py +++ b/snekbox.py @@ -2,7 +2,7 @@ import os import subprocess import sys -from flask import Flask, render_template, request, jsonify +from flask import Flask, jsonify, render_template, request class Snekbox: -- cgit v1.2.3