简体   繁体   English

使用PHP运行Python脚本

[英]Run Python script with PHP

I have this python file: 我有这个python文件:

light.py: light.py:

#!/usr/bin/python

import sys
import smbus
import time
from Adafruit_I2C import Adafruit_I2C


class Luxmeter:
i2c = None

def __init__(self, address=0x39, debug=0, pause=0.8):
    self.i2c = Adafruit_I2C(address)
    self.address = address
    self.pause = pause
    self.debug = debug
    self.gain = 0 # no gain preselected
    self.i2c.write8(0x80, 0x03)     # enable the device


def setGain(self,gain=1):
    """ Set the gain """
    if (gain != self.gain):
        if (gain==1):
            self.i2c.write8(0x81, 0x02)     # set gain = 1X and timing = 402 mSec
            if (self.debug):
                print "Setting low gain"
        else:
            self.i2c.write8(0x81, 0x12)     # set gain = 16X and timing = 402 mSec
            if (self.debug):
                print "Setting high gain"
        self.gain=gain;                     # safe gain for calculation
        time.sleep(self.pause)              # pause for integration (self.pause must be bigger than integration time)


def readWord(self, reg):
    """Reads a word from the I2C device"""
    try:
        wordval = self.i2c.readU16(reg)
        newval = self.i2c.reverseByteOrder(wordval)
        if (self.debug):
            print("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, wordval & 0xFFFF, reg))
        return newval
    except IOError:
        print("Error accessing 0x%02X: Check your I2C address" % self.address)
        return -1


def readFull(self, reg=0x8C):
    """Reads visible+IR diode from the I2C device"""
    return self.readWord(reg);

def readIR(self, reg=0x8E):
    """Reads IR only diode from the I2C device"""
    return self.readWord(reg);

def getLux(self, gain = 0):
    """Grabs a lux reading either with autoranging (gain=0) or with a specified gain (1, 16)"""
    if (gain == 1 or gain == 16):
        self.setGain(gain) # low/highGain
        ambient = self.readFull()
        IR = self.readIR()
    elif (gain==0): # auto gain
        self.setGain(16) # first try highGain
        ambient = self.readFull()
        if (ambient < 65535):
            IR = self.readIR()
        if (ambient >= 65535 or IR >= 65535): # value(s) exeed(s) datarange
            self.setGain(1) # set lowGain
            ambient = self.readFull()
            IR = self.readIR()

    if (self.gain==1):
       ambient *= 16    # scale 1x to 16x
       IR *= 16         # scale 1x to 16x

    if (float(ambient) != 0):
        ratio = (IR / float(ambient)) # changed to make it run under python 2
    else: ratio = 0

    if (self.debug):
        print "IR Result", IR
        print "Ambient Result", ambient

    if ((ratio >= 0) & (ratio <= 0.52)):
        lux = (0.0315 * ambient) - (0.0593 * ambient * (ratio**1.4))
    elif (ratio <= 0.65):
        lux = (0.0229 * ambient) - (0.0291 * IR)
    elif (ratio <= 0.80):
        lux = (0.0157 * ambient) - (0.018 * IR)
    elif (ratio <= 1.3):
        lux = (0.00338 * ambient) - (0.0026 * IR)
    elif (ratio > 1.3):
        lux = 0

    return lux


oLuxmeter=Luxmeter()

i=0
while True:
    light = oLuxmeter.getLux(1)
    if (light != 0):
        print light
        break
    else:
        i+=1
        if (i == 10):
            print light
            break

Now I want to run it in PHP on my Raspberry Pi with 现在我想用Raspberry Pi在PHP中运行它

echo system("/var/www/light.py")

but the response from the website ist nothing. 但是网站的回应却没有。 I give all files permissions with chmod+x, but it did not change anything. 我使用chmod + x授予所有文件权限,但是它没有任何改变。 If I type 如果我输入

python /var/www/light.py

into the console it works. 进入控制台即可。

The problem is that you're running the web server under some user that doesn't have privileges to use the smbus functions you're using. 问题是您正在以没有特权使用您正在使用的smbus功能的某些用户身份运行Web服务器。

You can test this by running something like su www-data /usr/bin/python /var/www/light.py (although the details will vary based on your settings, of course). 您可以通过运行su www-data /usr/bin/python /var/www/light.py (当然,详细信息会根据您的设置而有所不同)。 If that fails, you know this is your problem. 如果失败,则说明这是您的问题。 (Plus, you'll get to see the traceback, which can be helpful.) (此外,您将看到追溯,这可能会有所帮助。)

Running the web server as a user with as few privileges as possible is a great idea—but running it with fewer privileges than possible obviously isn't. 以尽可能少的特权以用户身份运行Web服务器是一个好主意-但以比可能少的特权运行它显然不是。 :) :)

Most privileges on *nix systems are controlled by user/group file permissions, so the answer is probably to add the web server user to the group that owns smbus . * nix系统上的大多数特权是由用户/组文件权限控制的,因此答案可能是将Web服务器用户添加到拥有smbus的组中。 As implied in the forum post you found, PHP exec and python-smbus , that group is usually named i2c , so if your web server user is named www-data , you'd run: 正如您在论坛帖子中暗示的那样, PHP exec和python-smbus ,该组通常名为i2c ,因此,如果您的Web服务器用户名为www-data ,则可以运行:

sudo adduser www-data i2c

As a side note, in the future, don't ignore the return value from calling another program. 附带说明一下,将来不要忽略调用另一个程序的返回值。 If it returns 1, or anything besides 0, that means the program has failed, and that's why you're not getting any useful output. 如果返回1或除0以外的任何值,则表明程序已失败,这就是为什么您未获得任何有用的输出的原因。 (Beyond 0 or not 0, the actual value isn't standardized, although 2 often means bad arguments.) (超出0或不等于0时,尽管2常常意味着错误的论点,但实际值并未标准化。)


Meanwhile, you've also got a second problem. 同时,您还有第二个问题。

As the docs for system explain, system doesn't return the output of the executed command. system文档所述, system不会返回已执行命令的输出。

The docs imply that you want to look at passthru , but that's probably not what you want either. 文档暗示您想查看passthru ,但这也不是您想要的。 Just like system , passthru dumps the output to your stdout rather than returning it; 就像system一样, passthru将输出转储到stdout而不是将其返回; the only difference is that it dumps it unfiltered by newline translation. 唯一的区别是,它转储了未经换行翻译的过滤器。

If you want to retrieve the output and then echo it, what you probably want here is exec , with an output argument: 如果要检索输出然后回显它,则可能需要在exec加上output参数:

If the output argument is present, then the specified array will be filled with every line of output from the command. 如果存在output参数,则命令的输出的每一行都将填充指定的数组。

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

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