Getting PHP to run a Python script

前端 未结 4 934
时光取名叫无心
时光取名叫无心 2020-12-06 05:24

I am trying to run a Python program using PHP. Here\'s the code

$command = \'/usr/local/bin/python script.py file\';
$temp = exec($command, $output);
         


        
相关标签:
4条回答
  • 2020-12-06 06:07

    Few checkpoints

    • script.py should be pass full path, eg, /home/abhinav/script.py
    • script.py should be executable, chmod +x script.py
    0 讨论(0)
  • 2020-12-06 06:07

    To get my CodeIgniter helper to run a python script, I had to put #!/usr/bin/python on the first line of my python script, and NOT call python from the exec.

    +1 for also doing chmod +x

    My helper looks like this:

    <?php
        function scrape($site, $key, $user_id)
        {
            $cmd = str_replace('system/','',BASEPATH).APPPATH."python/spider.py -u $site -k $key -i $user_id";
            $resp = exec($cmd, $out);
            return $out;
        }
    ?>
    
    0 讨论(0)
  • 2020-12-06 06:26

    You need to pass the full path to the script and you also need to make sure that the script is readable by the user running the web server (which means every directory in the path must be +x to the web user).

    0 讨论(0)
  • 2020-12-06 06:31

    Realized what was wrong:

    1. The domain was set up as a virtual host and PHP's safe_mode was enabled. proc_open, exec, system, passthru etc. do not work under safe_mode I guess.

    2. Put the script in the directory accessible by the vhost. Apache wasn't able to access the directories outside the vhost document root.

    Thanks for the help!

    0 讨论(0)
提交回复
热议问题