How to run python script command line in django view?

I have a .py file which php file works as follows:

$link = exec(dirname(__FILE__) . /xxx.py ' .excapeshellarg($url) . '2>&1', $output, $exit_code);

I want to run xxx.pydjango in my view and assign 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 to edit the file xxx.pyand call the main def function from my view?

Currently, it does not start, as it works on the command line when called directly from the view.

Thanks for your reply.

+3
source share
1 answer

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

. main , , stdout, , -, .

, , - :

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

Source: https://habr.com/ru/post/1732557/


All Articles