aboutsummaryrefslogtreecommitdiffstats
path: root/snekbox.py
diff options
context:
space:
mode:
authorGravatar Mark <[email protected]>2019-03-28 13:45:18 -0700
committerGravatar GitHub <[email protected]>2019-03-28 13:45:18 -0700
commitf8014521f7095aeba9344bb6c529ee57111d72e1 (patch)
tree2e61ab7e85b0cd31d80b60e88fdc7bf105bc0018 /snekbox.py
parentAdd Azure CI. (#16) (diff)
parentMerge remote-tracking branch 'origin' into rmq_removal (diff)
Replace RMQ with a POST endpoint (#7)
Diffstat (limited to 'snekbox.py')
-rw-r--r--snekbox.py64
1 files changed, 27 insertions, 37 deletions
diff --git a/snekbox.py b/snekbox.py
index f8d7c31..65fc4b3 100644
--- a/snekbox.py
+++ b/snekbox.py
@@ -1,10 +1,8 @@
-import json
-import multiprocessing
import os
import subprocess
import sys
-from rmq import Rmq
+from flask import Flask, jsonify, render_template, request
class Snekbox:
@@ -98,46 +96,38 @@ 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()
+
+# Load app
+app = Flask(__name__)
+app.use_reloader = False
+
+# Logging
+log = app.logger
+
- msg = body.decode('utf-8')
- result = ''
- snek_msg = json.loads(msg)
- snekid = snek_msg['snekid']
- snekcode = snek_msg['message'].strip()
+def index():
+ """Return a page with a form for inputting code to be executed."""
- result = self.python3(snekcode)
+ return render_template('index.html')
- 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]('/result', methods=["POST", "GET"])
+def result():
+ """Execute code and return a page displaying the results."""
- p = multiprocessing.Process(target=self.execute, args=(body,))
- p.daemon = True
- p.start()
+ if request.method == "POST":
+ code = request.form["Code"]
+ output = snekbox.python3(code)
+ return render_template('result.html', code=code, result=output)
- ch.basic_ack(delivery_tag=method.delivery_tag)
[email protected]('/input', methods=["POST"])
+def code_input():
+ """Execute code and return the results."""
-if __name__ == '__main__':
- try:
- rmq = Rmq()
- snkbx = Snekbox()
- rmq.consume(callback=snkbx.message_handler)
- except KeyboardInterrupt:
- print('Exited')
- exit(0)
+ body = request.get_json()
+ output = snekbox.python3(body["code"])
+ return jsonify(input=body["code"], output=output)