Executing dynamic python function in response to HTTP Call

Multi tool use
I have a http server written using bottle which has 2 URLS. Save/Update Code and Execute Code. I am passing a raw python file to save/update code and saving it in file system and returning a code id. I want to execute the code whenever a http request comes to /execute/{codeid}. I want to pass the parameters i received for the endpoint /execute/{codeid} to the raw python file i stored.
I am confused as to how to pass parameters to the dynamic python file. I am trying to create something similar to webtask.io using python.
I have tried to use code to create dynamic functions using the below given code. But it only creates a function. I want to import other modules also.
import types
import sys,imp
code = """def f(a,b,c):
print a+b+c, "really WoW"
"""
module = imp.new_module('myfunctions')
exec code in module.__dict__
module.f('W', 'o', 'W')
python bottle
add a comment |
I have a http server written using bottle which has 2 URLS. Save/Update Code and Execute Code. I am passing a raw python file to save/update code and saving it in file system and returning a code id. I want to execute the code whenever a http request comes to /execute/{codeid}. I want to pass the parameters i received for the endpoint /execute/{codeid} to the raw python file i stored.
I am confused as to how to pass parameters to the dynamic python file. I am trying to create something similar to webtask.io using python.
I have tried to use code to create dynamic functions using the below given code. But it only creates a function. I want to import other modules also.
import types
import sys,imp
code = """def f(a,b,c):
print a+b+c, "really WoW"
"""
module = imp.new_module('myfunctions')
exec code in module.__dict__
module.f('W', 'o', 'W')
python bottle
Found the solution. The solution can be found in the given gist url. If any suggestions for the below code i will be really happy @samaksh-jain gist.github.com/mdathersajjad/45eeca684a8ead6fe9173cbab3595e61
– AtherSajjad
Jan 2 at 13:33
add a comment |
I have a http server written using bottle which has 2 URLS. Save/Update Code and Execute Code. I am passing a raw python file to save/update code and saving it in file system and returning a code id. I want to execute the code whenever a http request comes to /execute/{codeid}. I want to pass the parameters i received for the endpoint /execute/{codeid} to the raw python file i stored.
I am confused as to how to pass parameters to the dynamic python file. I am trying to create something similar to webtask.io using python.
I have tried to use code to create dynamic functions using the below given code. But it only creates a function. I want to import other modules also.
import types
import sys,imp
code = """def f(a,b,c):
print a+b+c, "really WoW"
"""
module = imp.new_module('myfunctions')
exec code in module.__dict__
module.f('W', 'o', 'W')
python bottle
I have a http server written using bottle which has 2 URLS. Save/Update Code and Execute Code. I am passing a raw python file to save/update code and saving it in file system and returning a code id. I want to execute the code whenever a http request comes to /execute/{codeid}. I want to pass the parameters i received for the endpoint /execute/{codeid} to the raw python file i stored.
I am confused as to how to pass parameters to the dynamic python file. I am trying to create something similar to webtask.io using python.
I have tried to use code to create dynamic functions using the below given code. But it only creates a function. I want to import other modules also.
import types
import sys,imp
code = """def f(a,b,c):
print a+b+c, "really WoW"
"""
module = imp.new_module('myfunctions')
exec code in module.__dict__
module.f('W', 'o', 'W')
python bottle
python bottle
asked Jan 2 at 10:32
AtherSajjadAtherSajjad
6910
6910
Found the solution. The solution can be found in the given gist url. If any suggestions for the below code i will be really happy @samaksh-jain gist.github.com/mdathersajjad/45eeca684a8ead6fe9173cbab3595e61
– AtherSajjad
Jan 2 at 13:33
add a comment |
Found the solution. The solution can be found in the given gist url. If any suggestions for the below code i will be really happy @samaksh-jain gist.github.com/mdathersajjad/45eeca684a8ead6fe9173cbab3595e61
– AtherSajjad
Jan 2 at 13:33
Found the solution. The solution can be found in the given gist url. If any suggestions for the below code i will be really happy @samaksh-jain gist.github.com/mdathersajjad/45eeca684a8ead6fe9173cbab3595e61
– AtherSajjad
Jan 2 at 13:33
Found the solution. The solution can be found in the given gist url. If any suggestions for the below code i will be really happy @samaksh-jain gist.github.com/mdathersajjad/45eeca684a8ead6fe9173cbab3595e61
– AtherSajjad
Jan 2 at 13:33
add a comment |
1 Answer
1
active
oldest
votes
From what I understand, you are trying to execute code from a user and provide dynamic input.
Why not use something like: subprocess.call('python file_<codeid>.py <arg1> <arg2> <arg3>', shell=True)
You save the code in python file, then run it with arguments, get and validate the output from this.
You are correct i want to execute user provided code with dynamic input. Args can be string only right. I want to pass the request and response object itself to the user defined code. It is just going to be defining one method which will take the values from the request do some processing on it and write the result to the response stream.
– AtherSajjad
Jan 2 at 11:59
So did you try it withsubprocess.call()
?
– Samaksh Jain
Jan 2 at 12:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004740%2fexecuting-dynamic-python-function-in-response-to-http-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
From what I understand, you are trying to execute code from a user and provide dynamic input.
Why not use something like: subprocess.call('python file_<codeid>.py <arg1> <arg2> <arg3>', shell=True)
You save the code in python file, then run it with arguments, get and validate the output from this.
You are correct i want to execute user provided code with dynamic input. Args can be string only right. I want to pass the request and response object itself to the user defined code. It is just going to be defining one method which will take the values from the request do some processing on it and write the result to the response stream.
– AtherSajjad
Jan 2 at 11:59
So did you try it withsubprocess.call()
?
– Samaksh Jain
Jan 2 at 12:03
add a comment |
From what I understand, you are trying to execute code from a user and provide dynamic input.
Why not use something like: subprocess.call('python file_<codeid>.py <arg1> <arg2> <arg3>', shell=True)
You save the code in python file, then run it with arguments, get and validate the output from this.
You are correct i want to execute user provided code with dynamic input. Args can be string only right. I want to pass the request and response object itself to the user defined code. It is just going to be defining one method which will take the values from the request do some processing on it and write the result to the response stream.
– AtherSajjad
Jan 2 at 11:59
So did you try it withsubprocess.call()
?
– Samaksh Jain
Jan 2 at 12:03
add a comment |
From what I understand, you are trying to execute code from a user and provide dynamic input.
Why not use something like: subprocess.call('python file_<codeid>.py <arg1> <arg2> <arg3>', shell=True)
You save the code in python file, then run it with arguments, get and validate the output from this.
From what I understand, you are trying to execute code from a user and provide dynamic input.
Why not use something like: subprocess.call('python file_<codeid>.py <arg1> <arg2> <arg3>', shell=True)
You save the code in python file, then run it with arguments, get and validate the output from this.
answered Jan 2 at 11:11


Samaksh JainSamaksh Jain
35327
35327
You are correct i want to execute user provided code with dynamic input. Args can be string only right. I want to pass the request and response object itself to the user defined code. It is just going to be defining one method which will take the values from the request do some processing on it and write the result to the response stream.
– AtherSajjad
Jan 2 at 11:59
So did you try it withsubprocess.call()
?
– Samaksh Jain
Jan 2 at 12:03
add a comment |
You are correct i want to execute user provided code with dynamic input. Args can be string only right. I want to pass the request and response object itself to the user defined code. It is just going to be defining one method which will take the values from the request do some processing on it and write the result to the response stream.
– AtherSajjad
Jan 2 at 11:59
So did you try it withsubprocess.call()
?
– Samaksh Jain
Jan 2 at 12:03
You are correct i want to execute user provided code with dynamic input. Args can be string only right. I want to pass the request and response object itself to the user defined code. It is just going to be defining one method which will take the values from the request do some processing on it and write the result to the response stream.
– AtherSajjad
Jan 2 at 11:59
You are correct i want to execute user provided code with dynamic input. Args can be string only right. I want to pass the request and response object itself to the user defined code. It is just going to be defining one method which will take the values from the request do some processing on it and write the result to the response stream.
– AtherSajjad
Jan 2 at 11:59
So did you try it with
subprocess.call()
?– Samaksh Jain
Jan 2 at 12:03
So did you try it with
subprocess.call()
?– Samaksh Jain
Jan 2 at 12:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004740%2fexecuting-dynamic-python-function-in-response-to-http-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Z 0fxtS VIbyVC,n
Found the solution. The solution can be found in the given gist url. If any suggestions for the below code i will be really happy @samaksh-jain gist.github.com/mdathersajjad/45eeca684a8ead6fe9173cbab3595e61
– AtherSajjad
Jan 2 at 13:33