让 PHP 运行 Python 脚本

发布于 2024-08-20 19:04:58 字数 381 浏览 2 评论 0原文

我正在尝试使用 PHP 运行 Python 程序。这是代码,

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

它可以通过命令行运行,但不能在通过浏览器运行时运行。我正在使用 Apache,所以它可能需要正确的权限?我对 Linux 还很陌生,不知道如何让它工作。

任何帮助将不胜感激!

编辑1:

尝试使用proc_open但没有任何反应。我给出了脚本的完整路径。使脚本可执行但没有运气。我可以在服务器上尝试其他任何事情吗? (这是 CentOS 5)

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

折戟 2024-08-27 19:04:58

您需要将完整路径传递给脚本,还需要确保运行 Web 服务器的用户可以读取该脚本(这意味着路径中的每个目录对于 Web 用户都必须是 +x)。

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).

依 靠 2024-08-27 19:04:58

意识到出了什么问题:

  1. 域被设置为虚拟主机并且 PHP 的安全模式已启用。我猜 proc_open、exec、system、passthru 等在 safe_mode 下不起作用。

  2. 将脚本放在虚拟主机可访问的目录中。 Apache 无法访问虚拟主机文档根目录之外的目录。

感谢您的帮助!

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!

杀手六號 2024-08-27 19:04:58

少数检查点

  • script.py 应该传递完整路径,例如 /home/abhinav/script.py
  • script.py 应该是可执行的,chmod +x script.py

Few checkpoints

  • script.py should be pass full path, eg, /home/abhinav/script.py
  • script.py should be executable, chmod +x script.py
萌能量女王 2024-08-27 19:04:58

为了让我的 CodeIgniter 帮助程序运行 python 脚本,我必须将 #!/usr/bin/python 放在 python 脚本的第一行,而不是从 exec 调用 python。

+1 还执行 chmod +x

我的助手看起来像这样:

<?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;
    }
?>

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;
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文