php - 在 LINUX 服务器上从 PHP 启动 python 文件

标签 php python linux

我尝试了很多解决方案,但没有任何效果:

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/

相关文章:

php - cURL 和外部 Tor 中继

python - 有更简单的方法来检测 python 中的元音吗?

python - 如何将自定义变量值传递到 Django 管理界面?

java - Ubuntu 上的 Bad JetBrains Intellij IDEA 和 Android Studios CPU 使用率

linux - geniso 与 hfsbless 不工作

linux - 如何在 Linux shell 脚本中声明存储管道命令输出的变量

php - 如何使用PHP在露天上传图像文件

php - 提交表单时mysql如何发出电子邮件警报?

php - 阻塞方法如fcntl()和accept(),请求者中哪一个会得到它?

Python 3.x 四舍五入一半