开发者

How to run command line python script in django view?

I have a .py file that a php file runs like this:

$link = exec(dirname(__FILE__) . /xxx.py ' .excapeshellarg($url) . '2>&1', $output, 开发者_如何学Go$exit_code);

I want to run the xxx.py in my django view and asign the output to a variable. The xxx.py file has a def main(url) function and if __name__ == '__main__': at the bottom. Is there a way I can edit the xxx.py file and call the def main function from my view?

Currently it doesn't run like it runs on command line when called directly from the view.

Thanks for your response.


Is there a way I can edit the xxx.py file and call the def main function from my view?

Yes. Modify the main function so that it returns its results as a string rather than printing it to stdout, which is what it appears to be doing at the moment.

Then, from inside your view, you can do something like:

import xxx
results = xxx.main('foo')
# Do something with results
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜