从 PHP 运行 Python 脚本 [英] Running a Python script from PHP

查看:29
本文介绍了从 PHP 运行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下命令从 PHP 运行 Python 脚本:

exec('/usr/bin/python2.7/srv/http/assets/py/switch.py​​ arg1 arg2');

然而,PHP 根本不产生任何输出.错误报告设置为 E_ALL 并且 display_errors 开启.

这是我尝试过的:

  • 我使用了 python2/usr/bin/python2python2.7 而不是 /usr/bin/python2.7
  • 我还使用了相对路径而不是绝对路径,这也没有改变任何东西.
  • 我尝试使用命令 execshell_execsystem.

但是,如果我跑

if (exec('echo TEST') == 'TEST'){echo 'exec有效!';}

它工作得很好,而 shutdown now 什么也不做.

PHP 具有访问和执行文件的权限.

感谢 Alejandro,我能够解决这个问题.如果您遇到同样的问题,请不要忘记您的网络服务器可能/希望不会以 root 身份运行.尝试以您的网络服务器用户或具有类似权限的用户身份登录,并尝试自己运行命令.

解决方案

在 Ubuntu Server 10.04 上测试.我希望它也能帮助你使用 Arch Linux.

在 PHP 中 使用 shell_exec 函数:

<块引用>

通过shell执行命令并以字符串形式返回完整的输出.

如果有错误,则返回执行命令的输出或NULL发生或命令不产生输出.

进入 Python 文件 test.py,验证第一行的文本:(见shebang解释):

#!/usr/bin/env python

<块引用>

如果你安装了多个版本的 Python,/usr/bin/env 会确保使用的解释器是您环境中的第一个$路径.另一种方法是对类似的东西进行硬编码#!/usr/bin/python;没关系,但不太灵活.

在 Unix 中,一个要被解释的可执行文件可以表明通过 #! 使用什么解释器在第一行的开头,其次是解释器(以及它可能需要的任何标志).

如果你说的是其他平台,当然这个规则不申请(但shebang line"没有害处,如果你有帮助的话将该脚本复制到基于 Unix 的平台,例如 Linux,Mac 等).

<块引用>

这适用于您在 Unix 中通过使其可执行来运行它(chmod +x myscript.py) 然后直接运行:./myscript.py,而不仅仅是 python myscript.py

在 unix 类型平台上使文件可执行:

chmod +x myscript.py

还有 Python 文件 必须有正确的权限 (如果 PHP 脚本在浏览器或 curl 中运行,则为用户 www-data/apache 执行)和/或必须是可执行的".此外,.py 文件中的所有命令都必须具有正确的权限.

摘自来自php手册:

<块引用>

对于那些试图在unix 类型的平台,似乎无法让它工作.PHP 执行为系统上的网络用户(通常为 Apache 的 www),因此您需要确保网络用户有权访问任何文件或您尝试在 shell_exec 命令中使用的目录.否则,它似乎不会做任何事情.

I'm trying to run a Python script from PHP using the following command:

exec('/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2');

However, PHP simply doesn't produce any output. Error reporting is set to E_ALL and display_errors is on.

Here's what I've tried:

  • I used python2, /usr/bin/python2 and python2.7 instead of /usr/bin/python2.7
  • I also used a relative path instead of an absolute path which didn't change anything either.
  • I tried using the commands exec, shell_exec, system.

However, if I run

if (exec('echo TEST') == 'TEST')
{
    echo 'exec works!';
}

it works perfectly fine while shutdown now doesn't do anything.

PHP has the permissions to access and execute the file.

EDIT: Thanks to Alejandro, I was able to fix the problem. If you have the same problem, don't forget that your webserver probably/hopefully doesn't run as root. Try logging in as your webserver's user or a user with similar permissions and try to run the commands yourself.

解决方案

Tested on Ubuntu Server 10.04. I hope it helps you also on Arch Linux.

In PHP use shell_exec function:

Execute command via shell and return the complete output as a string.

It returns the output from the executed command or NULL if an error occurred or the command produces no output.

<?php 

$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;

?>

Into Python file test.py, verify this text in first line: (see shebang explain):

#!/usr/bin/env python

If you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is the first one on your environment's $PATH. The alternative would be to hardcode something like #!/usr/bin/python; that's ok, but less flexible.

In Unix, an executable file that's meant to be interpreted can indicate what interpreter to use by having a #! at the start of the first line, followed by the interpreter (and any flags it may need).

If you're talking about other platforms, of course, this rule does not apply (but that "shebang line" does no harm, and will help if you ever copy that script to a platform with a Unix base, such as Linux, Mac, etc).

This applies when you run it in Unix by making it executable (chmod +x myscript.py) and then running it directly: ./myscript.py, rather than just python myscript.py

To make executable a file on unix-type platforms:

chmod +x myscript.py

Also Python file must have correct privileges (execution for user www-data / apache if PHP script runs in browser or curl) and/or must be "executable". Also all commands into .py file must have correct privileges.

Taken from php manual:

Just a quick reminder for those trying to use shell_exec on a unix-type platform and can't seem to get it to work. PHP executes as the web user on the system (generally www for Apache), so you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.

这篇关于从 PHP 运行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆