无法从 PHP 文档执行 Python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32598420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't execute Python script from PHP document
提问by Bruh
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 pythonto 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:
我在 Raspberry Pi 上的 Apache 服务器上运行 PHP 文档,我希望它在单击按钮时运行一个文件。我在命令下放置了一些回显命令以使文件运行并打印出来但文件没有运行。index.php 文件和 lightson.py 和 Lightsoff.py 文件都在同一个目录(/var/www)中,我在这两个文件的顶部添加了#!/usr/bin/env python并使它们可执行使用chmod +x lightson.py。如果我从 shell 运行命令,它会工作并打开灯,就像我想要的一样,使用与文件中完全相同的命令,但它不会通过命令运行。编码:
<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>
<button name="LightOFF">Light OFF</button><br><br>
</form>
</html>
采纳答案by webdeb
as you said you are running it like apache->php->shell_exec(SUDO..)
正如你所说,你正在运行它 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
所以 apache 用户必须在 sudoers 文件中,最好不要给 apache sudo 而是给 apache (www-data) 用户运行你的 python 程序的权限
put first line in your python script: #!/usr/bin/env python
so the script knows which program to open it with..
把第一行放在你的python脚本中:#!/usr/bin/env python
这样脚本就知道用哪个程序打开它..
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-gpiopackage. This way you will have one place for your solid automation environment
提示:Apache 和 PHP 用于交付Documents and Strings
,如果您想要某种控制和 API,请从 nodejs 和https://www.npmjs.com/package/rpi-gpio包开始。这样,您将拥有一个适合您的可靠自动化环境的地方
回答by Jason
This worked for me:
这对我有用:
test.php
测试文件
<?php
echo shell_exec("python test.py");
?>
test.py
测试文件
f = open("test.txt", "a+")
f.write("hiya buddy!!\n")
f.close()
print "some output"
Here's my relevant ls -l
output from /var/www/html
:
这是我的相关ls -l
输出/var/www/html
:
jason@Jason-one /var/www/html $ ls -l
-rw-r--r-- 1 jason jason 44 Sep 20 18:12 test.php
-rwxr-xr-x 1 jason jason 82 Sep 20 17:44 test.py
-rw-rw-rw- 1 jason jason 38 Sep 20 18:15 test.txt
Since I don't have GPIO pins on my laptop, I decided to write to a file as a test. Notice I didn't have to use sudo
because of the way I set the permissions on test.py
.
由于我的笔记本电脑上没有 GPIO 引脚,因此我决定写入文件作为测试。请注意,sudo
由于我在 上设置权限的方式,我不必使用test.py
。