I have a .py file that a php file runs like this:
$link = exec(dirname(__FILE__) . /xxx.py ' .excapeshellarg($url) . '2>&1', $output, $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.
Yes. Modify the
mainfunction so that it returns its results as a string rather than printing it tostdout, which is what it appears to be doing at the moment.Then, from inside your view, you can do something like: