无法从PHP文件执行Python脚本 [英] Can't execute Python script from PHP document

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

问题描述

我正在我的树莓派一个Apache服务器上的PHP文件,我希望它是单击按钮时运行的文件。我把一些回声命令的指挥下使文件运行,并将其打印出,但该文件无法运行。 index.php文件和lightson.py和lightsoff.py文件都在同一目录下(的/ var / WWW )和我已经加入的#!的/ usr /斌/包膜蟒蛇以这两个文件的顶部,使他们的可执行文件使用搭配chmod + X lightson.py 。如果我从shell运行命令它的工作原理,并打开就像我想的完全一样的命令文件中的光,但尚未它不会通过命令来运行。在code:

 < HTML和GT;
< HEAD>
<标题>灯控< /标题>
< /头>
< PHP
如果(使用isset($ _ POST ['LightON']))
{
了shell_exec(sudo的蟒蛇/var/www/lightson.py);
回波(上);
}
如果(使用isset($ _ POST ['起燃']))
{
了shell_exec(sudo的蟒蛇/var/www/lightsoff.py);
回声(关);
}
?><形式方法=邮报>
<按钮名称=LightON>光针对< /按钮>&安培; NBSP;
<按钮名称=起燃>灯熄灭< /按钮>< BR>< BR>
< /表及GT;
< / HTML>


解决方案

你说你喜欢 apache-&GT运行它; PHP的>了shell_exec(SUDO ..)

因此​​Apache用户必须在sudoers文件,更好的你不给sudo来代替阿帕奇阿帕奇给运行你的Python程序(www数据)的用户权限

把第一行中的python脚本:<!code>#在/ usr /斌/包膜蟒蛇中的脚本,知道要打开它的程序。

然后

变化组:

  chgrp命令www数据/path/to/python-script.py

使其executabel

 搭配chmod + X /path/to/python-script.py

尝试

 了shell_exec(/路径/要/ python-script.py);

我希望它的工作原理;)

TIPP:Apache和PHP是提供文件和字符串,如果你想某种程度的控制和API开始的NodeJS和的 https://www.npmjs.com/package/rpi-gpio 包。这样,您将有一个地方你坚实的自动化环境

I am running a PHP document on an Apache server on my Raspberry Pi and I want it to run a file when a button is clicked. I put some echo commands under the command to make the file run and it prints out but the file doesn't run. The index.php file and lightson.py and lightsoff.py files are all in the same directory (/var/www) and I have added #!/usr/bin/env python to the top of both files and made them executable by using chmod +x lightson.py. If I run the command from the shell it works and turns on the light just like I want with the exact same command as in the file but yet it wont run through the command. The code:

<html>
<head>
<title>Light Controller</title>
</head>


<?php
if (isset($_POST['LightON']))
{
shell_exec("sudo python /var/www/lightson.py");
echo("on");
}
if (isset($_POST['LightOFF']))
{
shell_exec("sudo python /var/www/lightsoff.py");
echo("Off");
}
?>

<form method="post">
<button name="LightON">Light ON</button>&nbsp;
<button name="LightOFF">Light OFF</button><br><br>
</form> 


</html>

解决方案

as you said you are running it like apache->php->shell_exec(SUDO..)

So the apache user has to be in sudoers file, better you don't give sudo to apache instead give apache (www-data) user right to run your python program

put first line in your python script: #!/usr/bin/env python so the script knows which program to open it with..

then

change group:

chgrp www-data /path/to/python-script.py

make it executabel

chmod +x /path/to/python-script.py

try it

shell_exec("/path/to/python-script.py");

I hope it works ;)

TIPP: Apache and PHP are for delivering Documents and Strings, if you want some kind of control and an API start with nodejs and https://www.npmjs.com/package/rpi-gpio package. This way you will have one place for your solid automation environment

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

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