【问题标题】:Sense HAT CGI scriptSense HAT CGI 脚本
【发布时间】:2021-05-03 01:23:20
【问题描述】:

我正在尝试通过创建一个 CGI 脚本来从 Sense HAT 读取各种值,该脚本测量值并以 JSON 文档的形式返回它们。

这是脚本中运行的代码。

#!/usr/bin/python3

# Script to read sensor data from the sense hat and return it as a JSON document

import sense_hat
import json
import cgitb

cgitb.enable()

# Set up sensehat.
sense = sense_hat.SenseHat()

# Calling these functions because the get functions will sometimes return zero when used for the first time.
sense._init_humidity()
sense._init_pressure()

# Set up json document
output = {
    "humidity": sense.get_humidity(),
    "pressure": sense.get_pressure(), 
    "temperature" : sense.get_temperature()
}

# Encode json to string and return to STDOUT
print("Content-Type: application/json\n")
print(json.dumps(output))

我已将 apache2 网络服务器设置为接受 /var/www/cgi-bin 文件夹中的 .py 文件。一个简单的 hello world 已验证这是有效的。

尝试通过浏览器访问脚本时,会报告内部服务器错误。我以 www-data(运行 cgi-scripts 的用户)的身份手动执行了脚本,我认为该用户没有足够的权限来使用 sense hat。

Traceback (most recent call last):
  File "./throw.py", line 12, in <module>
    sense = sense_hat.SenseHat()
  File "/usr/lib/python3/dist-packages/sense_hat/sense_hat.py", line 92, in __init__
    self._stick = SenseStick()
  File "/usr/lib/python3/dist-packages/sense_hat/stick.py", line 57, in __init__
    self._stick_file = io.open(self._stick_device(), 'rb', buffering=0)
PermissionError: [Errno 13] Permission denied: '/dev/input/event0'

我怎样才能最好地做到这一点?

【问题讨论】:

    标签: python raspberry-pi


    【解决方案1】:

    您可能只需要授予 Apache 服务器使用适当权限的任何用户。

    请参阅this answer 以获得修复。

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 2014-05-31
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 2012-03-10
      相关资源
      最近更新 更多