diff options
author | 2018-05-23 22:38:20 +0200 | |
---|---|---|
committer | 2018-05-23 22:38:20 +0200 | |
commit | 309a6f93f878fc96951902fc47d45a30ef5f8d71 (patch) | |
tree | f788b43a892a93d0f97da73f459a55b43e1ea1a0 /snekbox.py | |
parent | update readme (diff) |
POC completed
Diffstat (limited to 'snekbox.py')
-rw-r--r-- | snekbox.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/snekbox.py b/snekbox.py new file mode 100644 index 0000000..b58447e --- /dev/null +++ b/snekbox.py @@ -0,0 +1,49 @@ +import traceback +import sys +import time +import pika +import io + +from rmq.consumer import consume +from rmq.publisher import publish + +from config import USERNAME +from config import PASSWORD +from config import HOST +from config import PORT +from config import EXCHANGE +from config import EXCHANGE_TYPE +from config import QUEUE +from config import RETURN_QUEUE +from config import RETURN_EXCHANGE +from config import RETURN_ROUTING_KEY + +def execute(snippet): + old_stdout = sys.stdout + redirected_output = sys.stdout = io.StringIO() + failed = False + try: + exec(snippet) + except Exception as e: + failed = str(e) + finally: + sys.stdout = old_stdout + + if failed: + return failed.strip() + return redirected_output.getvalue().strip() + + +def message_handler(ch, method, properties, body): + msg = body.decode('utf-8') + + # Execute code snippets here + print(f"incoming: {msg}", flush=True) + result = execute(msg) + print(f"outgoing: {result}", flush=True) + publish(result, host=HOST, queue=RETURN_QUEUE, routingkey=RETURN_ROUTING_KEY, exchange=RETURN_EXCHANGE, exchange_type=EXCHANGE_TYPE) + + ch.basic_ack(delivery_tag = method.delivery_tag) + +if __name__ == '__main__': + consume(host=HOST, queue=QUEUE, callback=message_handler) |