Отказано в доступе, Python, CGI, Apache

Привет, я пытаюсь запустить этот код Python в моем /usr/lib/cgi-bin/mcp3008.py. Я работаю с Raspberry PI и сервером Apache. Я новичок в этом, спасибо

    #!/usr/bin/env python
#--------------------------------------
# This script reads data from a
# MCP3008 ADC device using the SPI bus.
#
# Author : Matt Hawkins
# Date   : 13/10/2013
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------

import spidev
import time
import os

print "Content-type: text/html\n\n"
print "<bold>Light:</bold>"
# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data

# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
  volts = (data * 3.3) / float(1023)
  volts = round(volts,places)
  return volts

# Function to calculate temperature from
# TMP36 data, rounded to specified
# number of decimal places.
# TMP36 data, rounded to specified
# number of decimal places.
def ConvertTemp(data,places):

  # ADC Value
  # (approx)  Temp  Volts
  #    0      -50    0.00
  #   78      -25    0.25
  #  155        0    0.50
  #  233       25    0.75
  #  310       50    1.00
  #  388       75    1.25
  #  465      100    1.50
  #  543      125    1.75
  #  620      150    2.00
  #  698      175    2.25
  #  775      200    2.50
  #  853      225    2.75
  #  930      250    3.00
  # 1008      275    3.25
  # 1023      280    3.30

  temp = ((data * 330)/float(1023))-50
  temp = round(temp,places)
  return temp

# Define sensor channels
light_channel = 0
temp_channel  = 1

# Define delay between readings
delay = 5

while True:

  # Read the light sensor data
  light_level = ReadChannel(light_channel)
  light_volts = ConvertVolts(light_level,2)
 light_volts = ConvertVolts(light_level,2)

  # Read the temperature sensor data
  temp_level = ReadChannel(temp_channel)
  temp_volts = ConvertVolts(temp_level,2)
  temp       = ConvertTemp(temp_level,2)

  # Print out results
  print "------The 'Ponics Project--------------------------------------"
  print("Light : {} ({}V)".format(light_level,light_volts))
  print("Temperature in the room  is: {} ({}V) {} deg F".format(temp_level,temp_volts,(temp * 9/5)+ 32))
  # Wait before repeating loop
  time.sleep(delay)

Проблема в том, что я получаю ошибку. Вот как выглядит мой var/log/apache2/error.log

[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1] Traceback (most recent call last):
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1]   File "/usr/lib/cgi-bin/mcp3008.py", line 21, in <module>
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1]
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1] spi.open(0,0)
[Wed Apr 29 21:15:37 2015] [error] [client 192.168.2.1] IOError: [Errno 13] Permission denied
[Wed Apr 29 21:15:38 2015] [error] [client 192.168.2.1] File does not exist: /var/www/favicon.ico, referer: http://192.168.2$

person frafrichile    schedule 30.04.2015    source источник
comment
Запустили ли вы скрипт с помощью sudo (например, sudo python ‹scriptname.py›)   -  person Steve Robillard    schedule 01.05.2015
comment
Да, я пробовал sudo adduser www-data spi, но группа SPI не существует.   -  person frafrichile    schedule 05.05.2015
comment
@frafrichile, вы нашли решение для этого?   -  person Cosmin    schedule 05.02.2019


Ответы (1)


Чтобы ваши веб-программы, работающие на веб-сервере Apache, могли взаимодействовать с устройствами SPI, вам необходимо предоставить учетной записи пользователя, которая запускает веб-сервер Apache, разрешения для устройств SPI. Введите эту команду:

sudo usermod -a -G spi www-данные

Чтобы изменения вступили в силу, необходимо перезагрузить PI.

person Philipp H.    schedule 11.09.2016