我尝试了很多解决方案,但没有任何效果:
echo '<pre>';
shell_exec("python /home/folder/python/mapfile_compress.py");
shell_exec("sudo -u wwwexec python escapeshellcommand(/home/folder/python/mapfile_compress.py) $uid");
shell_exec("sudo chmod +x /home/folder/python/mapfile_compress.py");
system("bash /home/folder/python/mapfile_compress.py");
passthru("bash /home/folder/python/mapfile_compress.py");
passthru("/home/folder/python/mapfile_compress.py");
exec("bash /home/folder/python/mapfile_compress.py");
echo '</pre>';
我分别启动了它们,但在所有情况下,Firebug 都返回:'<pre>'
所以我尝试了这段基于 Stack Overflow 的代码:
$command = escapeshellcmd('chmod +x /home/folder/python/mapfile_compress_test.py');
echo $command;
$output = shell_exec($command);
echo $output;
但是 Firebug 什么也没返回。
我的 python 文件以 #!/usr/bin/env python
开头如果我在可用的服务器上启动它!
您知道如何从 PHP 文件启动我的 python 文件吗?
最佳答案
chmod
成功时返回 0,错误时返回 > 0。
确保该文件能够通过以网络用户身份运行来运行。正确设置 +x 后,您只需调用 $/path/to/your/file.py
即可执行它,脚本第一行的 shebang #!/usr/bin/env python
应该根据您的环境定义正确的 python。
您可以通过运行来测试它:
$ /usr/bin/env python /path/to/your/file.py
因此请检查您的文件权限,以检查该文件是否可由运行 php 脚本的用户执行。
只是为了测试,你可以在你的python文件中打印几行
#!/usr/bin/env python
print "test line 1"
print "test line 2"
然后如果你已经验证了权限并且正确使用了python,你可以在你的php中这样做。
$command = escapeshellcmd('/path/to/your/file.py');
$output = shell_exec($command); // get all output or use passthrough, exec will only return the last line.
echo "<pre>{$output}</pre>;
关于php - 在 LINUX 服务器上从 PHP 启动 python 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44819073/