简体   繁体   English

权限被拒绝,Python,CGI,Apache

[英]Permission denied,Python , CGI ,Apache

Hi I am trying to run this python code in my /usr/lib/cgi-bin/mcp3008.py I am working with a raspberry PI and an Apache server. 嗨,我正尝试在我的/usr/lib/cgi-bin/mcp3008.py中运行此python代码。我正在使用树莓派PI和Apache服务器。 I am pretty new to this, Thank you 我对此很陌生,谢谢

    #!/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)

The problem is that I get an error. 问题是我得到一个错误。 This is what my var/log/apache2/error.log looks like 这就是我的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$

To allow your web programs running on the Apache web server to talk to the SPI devices, you'll need to give the user account that runs the Apache web server permissions for the SPI devices. 为了允许您在Apache Web服务器上运行的Web程序与SPI设备通信,您需要为运行Apache Web服务器的用户帐户提供SPI设备的权限。 Enter this command: 输入此命令:

sudo usermod -a -G spi www-data 须藤usermod -a -G spi www-data

You need to reboot the PI before changes take effect 您需要重新引导PI才能使更改生效

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

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