Executing a Python file from Codeigniter

。_饼干妹妹 提交于 2021-02-08 04:12:01

问题


I have some trouble to execute a Python file from a Codeigniter controller's function.

~/application/controllers/Lights.php :

header("Access-Control-Allow-Origin: *");
class Lights extends CI_Controller {
    public function __construct() {
        parent::__construct ();
        $this->load->helper ( array ('url', 'form') );
    }
    public function turnOn() {
        system('sudo python test.py > /dev/null 2>/dev/null &');
    }
}

but when I try to execute a system call, like :

system("sudo gpio mode 15 out");

this works perfectly.


回答1:


Provide the full path to python binary and script, i.e.:

system('/full/path/to/python /full/path/to/test.py > /dev/null 2>/dev/null &');

If the python binary is already on the PATH of your apache user (apache/www-user), you may ignore the full path.




回答2:


yes I has a problem like that and Thank you for Pedro Lobito answer. There need a full path to call python file from CI from the root folder.

such as

system('sudo -u root -S python /var/www/4led/turnon1.py');

*sudo -u root -S is a command for using root permission with no root password required :D



来源:https://stackoverflow.com/questions/37093408/executing-a-python-file-from-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!