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

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

问题描述

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

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');

但是,PHP根本不产生任何输出.错误报告设置为E_ALL,并且display_errors处于打开状态.

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

这是我尝试过的:

  • 我用python2/usr/bin/python2python2.7代替了/usr/bin/python2.7
  • 我还使用了相对路径而不是绝对路径,该绝对路径也没有任何改变.
  • 我尝试使用命令execshell_execsystem.
  • 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.

但是,如果我跑步

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

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

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

PHP有权访问和执行文件.

PHP has the permissions to access and execute the file.

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

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.

推荐答案

在Ubuntu Server 10.04上进行了测试.我希望它也可以在Arch Linux上为您提供帮助.

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

在PHP中使用shell_exec函数:

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

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

它返回已执行命令的输出,如果出错则返回NULL 发生或命令没有输出.

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;

?>

在Python文件test.py中,在第一行中验证以下文本:

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

#!/usr/bin/env python

Python文件必须具有正确的权限(如果PHP脚本在浏览器或curl中运行,则对用户www-data/apache执行) 和/或必须是可执行的".同样,所有进入.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:

从php手册中摘录

Taken from php manual:

对于那些试图在shell上使用shell_exec的人来说,这只是一个简短的提醒. unix型平台,似乎无法使其正常工作. PHP执行为 系统上的Web用户(对于Apache通常为www),因此您需要 确保网络用户有权访问任何文件或 您要在shell_exec命令中使用的目录. 否则,它似乎什么也没做.

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.

在Unix类型平台上将可执行文件制成文件:

chmod +x myscript.py

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

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