我正在尝试从某种意义上读取各种值,方法是创建一个CGI脚本来测量这些值,并以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 set服务器设置为接受/var/www/cgi-bin文件夹中的.py文件。一个简单的hello world已经验证了这是有效的。
尝试通过浏览器访问脚本时,系统会报告内部服务器错误。我以www-data (运行cgi脚本的用户)身份手动执行该脚本,我认为该用户没有足够的权限来使用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'我该怎么做才是最好的呢?
发布于 2021-05-03 01:23:21
您可能只需要为Apache服务器使用的任何用户提供适当的权限。
请参阅this answer以获取修复。
https://stackoverflow.com/questions/67357430
复制相似问题