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);
This works through the command line but not while running it through the browser. I am using Apache so probably it needs the right privileges? I am pretty new to Linux and have no idea how to get this working.
Any help would be appreciated!
Edit 1:
Tried to use proc_open
but nothing happens. I gave the full path to the script. Made the script executable but no luck. Any other things I can try on the server? (It's a CentOS 5)
您需要传递脚本的完整路径,并且还需要确保运行Web服务器的用户可以读取脚本(这意味着路径中的每个目录必须是Web用户的+ x)。
Realized what was wrong:
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.
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!
Few checkpoints
/home/abhinav/script.py
chmod +x script.py
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;
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.