Executing dynamic python function in response to HTTP Call

Multi tool use
Multi tool use












1















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')









share|improve this question























  • 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
















1















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')









share|improve this question























  • 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














1












1








1








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')









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer
























  • 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













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
});


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer
























  • 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


















0














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.






share|improve this answer
























  • 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
















0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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 with subprocess.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













  • So did you try it with subprocess.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






















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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
AaL3xqU,5OpgbWG3CrQxem

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas