From 3e9b2c2a0eae3a614a3ea3e3ac8670f380cba04e Mon Sep 17 00:00:00 2001 From: prashantsinalkar Date: Sat, 29 Jun 2019 15:26:46 +0530 Subject: added user id and clean the code --- instances.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/instances.py b/instances.py index c6f4f1a..c11aca9 100644 --- a/instances.py +++ b/instances.py @@ -9,6 +9,7 @@ import requests import json import urllib.request import base64 +import uuid from datetime import datetime from django.template.loader import render_to_string, get_template @@ -19,12 +20,13 @@ from R_on_Cloud.config import (BIN) URL = "http://127.0.0.1:8001/rscript" + def execute_code(code, token): # Check for system commands system_commands = re.compile( - 'unix\(.*\)|unix_g\(.*\)|unix_w\(.*\)|' - 'unix_x\(.*\)|unix_s\(.*\)|host|newfun' - '|execstr|ascii|mputl|dir\(\)' + r'unix\(.*\)|unix_g\(.*\)|unix_w\(.*\)|' + r'unix_x\(.*\)|unix_s\(.*\)|host|newfun' + r'|execstr|ascii|mputl|dir\(\)' ) if system_commands.search(code): return { @@ -33,28 +35,26 @@ def execute_code(code, token): code = re.sub(r"View\(", "print(", code) - - - - body = {'code': code} + session_id = uuid.uuid4() + user_id = uuid.uuid4() + body = {'code': code, + 'user_id': user_id + } req = urllib.request.Request(URL) req.add_header('Content-Type', 'application/json; charset=utf-8') jsondata = json.dumps(body) - jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes always as it R code + jsondataasbytes = jsondata.encode('utf-8') req.add_header('Content-Length', len(jsondataasbytes)) - print (jsondataasbytes) + print(jsondataasbytes) result = urllib.request.urlopen(req, jsondataasbytes) result = result.read().decode("utf8") - print("----------", result) - #print("----", json.loads(result)['value']) output = json.loads(result)["output"] - print ("-------------------------------", output) - #output = "okk" data = { 'output': output } return data -#https://github.com/trestletech/plumber/issues/105 + + def trim(output): output = [line for line in output.split('\n') if line.strip() != ''] output = '\n'.join(output) -- cgit