繁体   English   中英

使用浏览器在 php 脚本中调用时,Raspberry Pi Python 代码无法正确执行

[英]Raspberry Pi Python code not executing properly when called in php script using a browser

我有一个运行 Raspbian 10 (buster) 的 Raspberry Pi 1B,安装了 Python3、Apache2 和 PHP,并安装了一条 WS2812 LED。

我有一个简单的 Python 脚本(PixelTestOn.py,见下文)可以打开所有 LED。 使用以下终端命令时,LED 全部亮起,脚本中的文本按预期显示:

sudo python3 PixelTestOn.py*

PixelTestOn.py

#! /usr/bin/env python3

print("PixelTestOn started<br>")

from rpi_ws281x import PixelStrip, Color

# LED strip configuration:
LED_COUNT = 30        # Number of LED pixels.
LED_PIN = 18          # GPIO pin connected to the pixels (18 uses PWM!).
# LED_PIN = 10        # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10          # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 40   # Set to 0 for darkest and 255 for brightest
LED_INVERT = True     # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0       # set to '1' for GPIOs 13, 19, 41, 45 or 53

# Create NeoPixel object with appropriate configuration.
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)

# Intialize the library (must be called once before other functions).
strip.begin()

# Switch all pixels on
for i in range(strip.numPixels()):
    strip.setPixelColor(i, Color(255, 255, 255))
strip.show()

print("PixelTestOn finished")

exit()

我还有一个 php 脚本(PixelTestOn.php,见下文),它应该显示一些文本,执行 PixelTestOn.py python 脚本来打开 LED,然后显示更多的文本。 使用以下终端命令时,将显示 HTML 代码,并且 LED 全部亮起,并显示 python 脚本中的文本,一切如预期:

sudo php PixelTestOn.php

PixelTestOn.php

<html>
<head>
<title>Pixel Test</title>

<h3>Test to turn LEDs on</h3>
<p>Should see all LEDs light up</p>
<p></p>
<p>About to call shell_exec() (5)</p>

<?PHP
echo shell_exec("python PixelTestOn.py");
?>

<p>Returned from shell_exec()</p>

</head>

但是,当我在 Chromium 浏览器中输入以下内容时,将显示 html 输出(不是代码),并且 Python 代码开头的第一个 print() 语句中的文本按预期显示,但是,LED 不亮并且不显示python代码末尾的第二个print()语句中的文本:

localhost/PixelTestOn.php

如果我注释掉 2 个 print() 语句之间的所有语句,则脚本将在浏览器中按预期执行。

所有文件都存储在 /var/www/html 目录中,所有文件的所有者都是 www-data,并且都将访问控制设置为“任何人”。

这是我尝试过的:

  • 在 php 脚本中使用 shell_exec()、exec() 和 system()
  • 在我的 Windows 10 PC 和 Android 平板电脑上使用不同的浏览器(Echo、IE、Chrome)。 请注意,在 Pi 上启用了 SSH。
  • 两个不同的 LED 控制库,rpi_ws281x 和 neopixel
  • 通用网关接口 (CGI)。 我无法使用带有 [提交] 按钮的基于 html 的表单来使用它,当按下按钮输入数据时,浏览器中会显示“内部服务器错误”消息。

我的最终目的是构建一个基于无头 Raspberry Pi 的电子板球记分牌,带有可单独寻址的 LED,可通过 Android 平板电脑远程访问。

更新按照 Jay 的评论,我尝试在终端中运行“python3 PixelTestOn.py”即没有“sudo”,并在打印语句“PixelTestOn 启动”后收到以下错误消息
' 显示:

Can’t open /dev/mem: Permission denied
Traceback (most recent call last):
  File “PixelTestOn.py”, line 24, in <module>
    strip.begin()
  File “/home/pi/.local/lib/python3.7/site-packages/rpi_ws281x/rpi_ws281x.py”, line 130, in begin
    raise RuntimeError(‘ws2811_init failed with code {0} ({1})’.format(resp, str_resp))
RuntimeError: ws2811_init failed with code -5 (mmap() failed)

看到“权限被拒绝”语句,我记下了文件“/dev/mem”的权限,然后输入以下内容将所有 3 个选项的访问控制更改为“任何人”。

sudo 777 /dev/mem

然后我在终端中重新运行 'python3 PixelTestOn.py',再次在打印语句 'PixelTestOn 开始后
' 显示,收到类似的错误消息,但这次从:

Can’t open /dev/mem: Operation not permitted

然后我将 /dev/mem 的权限改回原来的设置

欢迎使用堆栈溢出。

根据以下链接,我看到您无法避免以 root 身份运行 python 脚本 - https://github.com/jgarff/rpi_ws281x/issues/155#issuecomment-370939228

所以你需要在下面做,当然 -

echo shell_exec("sudo python PixelTestOn.py"); # Note the 'sudo' prefix

此外,当您通过浏览器(如 Chromium 或 Firefox 等)运行 PHP 时,请求必须通过您的网络服务器(Apache、NginX 等)。 因此,您的 PHP 脚本将以 Web 用户而非 root 用户身份执行。

假设,根据您的问题,您的网络用户是www-data ,请在您的/etc/sudoers文件中添加以下文本,以允许www-data以 sudo 身份运行命令 -

www-data ALL=(ALL) NOPASSWD: ALL

这应该可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM