从PHP运行Python脚本。

时间:2022-08-21 17:08:49

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

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

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

exec(“/ usr / bin / python2.7 /电脑/ http /资产/ py /开关。py __arg1最长的);

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

但是,PHP根本不会产生任何输出。错误报告设置为E_ALL,并启用display_errors。

Here's what I've tried:

这是我试过:

  • I used python2, /usr/bin/python2 and python2.7 instead of /usr/bin/python2.7
  • 我用了python2 /usr/ binon2和python2.7代替了/usr/ binon2.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.
  • 我尝试使用命令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.

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

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.

编辑:多亏了亚历杭德罗,我才解决了这个问题。如果您有同样的问题,请不要忘记您的webserver可能/希望不会作为root用户运行。尝试作为webserver的用户或具有类似权限的用户登录,并尝试自己运行命令。

6 个解决方案

#1


107  

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

在Ubuntu服务器10.04上测试。我希望它在Arch Linux上也能帮助您。

In PHP:

在PHP中:

<?php 

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

?>

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

在Python文件测试。py,在第一行验证文本:(见shebang explain):

#!/usr/bin/env python

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

另外,Python文件应该具有正确的特权(如果PHP脚本在浏览器中运行,或者通过curl运行)和/或必须是“可执行的”,那么它应该具有正确的权限。而且。py文件中的所有命令都必须具有正确的权限。

chmod +x myscript.py

#2


15  

I recommend using passthru and handling the output buffer directly:

我建议使用passthru直接处理输出缓冲区:

ob_start();
passthru('/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2');
$output = ob_get_clean(); 

#3


11  

If you want to know the return status of the command and get the entire stdout output you can actually use exec:

如果您想知道命令的返回状态并获得整个stdout输出,您可以使用exec:

$command = 'ls';
exec($command, $out, $status);

$out is an array of all lines. $status is the return status. Very useful for debugging.

$out是所有行的数组。$status是返回状态。对调试非常有用。

If you also want to see the stderr output you can either play with proc_open or simply add 2>&1 to your $command. The latter is often sufficient to get things working and way faster to "implement".

如果您还想查看stderr输出,您可以使用proc_open,也可以在$命令中添加2>和1。后者通常足以使工作和方法更快地“实现”。

#4


8  

Alejandro nailed it, adding clarification to the exception (Ubuntu or Debian) - I don't have the rep to add to the answer itself:

亚历杭德罗(Alejandro)做到了这一点,并对这个例外(Ubuntu或Debian)做了澄清——我没有资格对这个问题的答案进行补充:

sudoers file: sudo visudo

sudoers文件:sudo visudo

exception added: www-data ALL=(ALL) NOPASSWD: ALL

例外添加:www-data ALL=(ALL) NOPASSWD: ALL

#5


2  

In my case I needed to create a new folder in the www directory called scripts. Within scripts I added a new file called test.py.

在我的例子中,我需要在名为scripts的www目录中创建一个新的文件夹。在脚本中,我添加了一个名为test.py的新文件。

I then used sudo chown www-data:root scripts and sudo chown www-data:root test.py.

然后我使用了sudo chown www-data:根脚本和sudo chown www-data:root test.py。

Then I went to the new scripts directory and used sudo chmod +x test.py.

然后我转到新的脚本目录,使用sudo chmod +x test.py。

My test.py file it looks like this. Note the different Python version:

我的测试。py文件是这样的。注意不同的Python版本:

#!/usr/bin/env python3.5
print("Hello World!")

From php I now do this:

从php开始,我现在这样做:

$message = exec("/var/www/scripts/test.py 2>&1");
print_r($message);

And you should see: Hello World!

你应该看看:你好,世界!

#6


0  

This is so trivial, but just wanted to help anyone who already followed along Alejandro's suggestion but encountered this error:

这是如此微不足道,但只是想帮助那些已经遵循亚历杭德罗的建议但遇到了这个错误的人:

sh: blabla.py: command not found

承宪:鼓励性。py:命令没有找到

If anyone encountered that error, then a little change needs to be made to the php file by Alejandro:

如果有人遇到这个错误,那么Alejandro需要对php文件做一点修改:

$command = escapeshellcmd('python webScraping.py');

#1


107  

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

在Ubuntu服务器10.04上测试。我希望它在Arch Linux上也能帮助您。

In PHP:

在PHP中:

<?php 

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

?>

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

在Python文件测试。py,在第一行验证文本:(见shebang explain):

#!/usr/bin/env python

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

另外,Python文件应该具有正确的特权(如果PHP脚本在浏览器中运行,或者通过curl运行)和/或必须是“可执行的”,那么它应该具有正确的权限。而且。py文件中的所有命令都必须具有正确的权限。

chmod +x myscript.py

#2


15  

I recommend using passthru and handling the output buffer directly:

我建议使用passthru直接处理输出缓冲区:

ob_start();
passthru('/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2');
$output = ob_get_clean(); 

#3


11  

If you want to know the return status of the command and get the entire stdout output you can actually use exec:

如果您想知道命令的返回状态并获得整个stdout输出,您可以使用exec:

$command = 'ls';
exec($command, $out, $status);

$out is an array of all lines. $status is the return status. Very useful for debugging.

$out是所有行的数组。$status是返回状态。对调试非常有用。

If you also want to see the stderr output you can either play with proc_open or simply add 2>&1 to your $command. The latter is often sufficient to get things working and way faster to "implement".

如果您还想查看stderr输出,您可以使用proc_open,也可以在$命令中添加2>和1。后者通常足以使工作和方法更快地“实现”。

#4


8  

Alejandro nailed it, adding clarification to the exception (Ubuntu or Debian) - I don't have the rep to add to the answer itself:

亚历杭德罗(Alejandro)做到了这一点,并对这个例外(Ubuntu或Debian)做了澄清——我没有资格对这个问题的答案进行补充:

sudoers file: sudo visudo

sudoers文件:sudo visudo

exception added: www-data ALL=(ALL) NOPASSWD: ALL

例外添加:www-data ALL=(ALL) NOPASSWD: ALL

#5


2  

In my case I needed to create a new folder in the www directory called scripts. Within scripts I added a new file called test.py.

在我的例子中,我需要在名为scripts的www目录中创建一个新的文件夹。在脚本中,我添加了一个名为test.py的新文件。

I then used sudo chown www-data:root scripts and sudo chown www-data:root test.py.

然后我使用了sudo chown www-data:根脚本和sudo chown www-data:root test.py。

Then I went to the new scripts directory and used sudo chmod +x test.py.

然后我转到新的脚本目录,使用sudo chmod +x test.py。

My test.py file it looks like this. Note the different Python version:

我的测试。py文件是这样的。注意不同的Python版本:

#!/usr/bin/env python3.5
print("Hello World!")

From php I now do this:

从php开始,我现在这样做:

$message = exec("/var/www/scripts/test.py 2>&1");
print_r($message);

And you should see: Hello World!

你应该看看:你好,世界!

#6


0  

This is so trivial, but just wanted to help anyone who already followed along Alejandro's suggestion but encountered this error:

这是如此微不足道,但只是想帮助那些已经遵循亚历杭德罗的建议但遇到了这个错误的人:

sh: blabla.py: command not found

承宪:鼓励性。py:命令没有找到

If anyone encountered that error, then a little change needs to be made to the php file by Alejandro:

如果有人遇到这个错误,那么Alejandro需要对php文件做一点修改:

$command = escapeshellcmd('python webScraping.py');