Lighttpd cgi python 无法 运行 系统进程

Lighttpd cgi python fail to run system processes

我正在尝试从 Web python 脚本中 运行 终端命令。 我尝试了很多东西,但 none 似乎有效...例如:将 'www-data' 添加到 sudoers,使用 bin 的完整路径,运行 命令与 sudo word,使用 3 个不同的系统调用( os.spawnl 和子进程)和 none 的作品。 只读命令,如 "ps aux" 只输出信息有效,但简单的回显到文件则无效。看起来需要许可才能这样做。我还能尝试什么?

输出示例:意外错误:(, CalledProcessError(2, '/bin/echo hello > /var/www/html/cgi-bin/test2.htm'), )

在该示例中,/var/www/html/cgi-bin/ 文件夹归 "www-data" 所有,用户与服务器配置相同。

<!-- language: python -->
#!/usr/bin/python3
# coding=utf-8
import os
import sys
import subprocess
import cgi
import subprocess

SCRIPT_PATH = "/var/www/html/scripts/aqi3.py"
DATA_FILE = "/var/www/html/assets/aqi.json"
KILL_PROCESS = "ps aux | grep " + SCRIPT_PATH + " | grep -v \"grep\" | awk '{print }' | xargs kill -9"
START_PROCESS = "/usr/bin/python3 " + SCRIPT_PATH + " start > /dev/null 2>&1 &"
STOP_PROCESS = "/usr/bin/python3 " + SCRIPT_PATH + " stop > /dev/null 2>&1 &"


# Don't edit
def killProcess():
    os.spawnl(os.P_NOWAIT, KILL_PROCESS)
    try:
        os.spawnl(os.P_NOWAIT, "/bin/echo hello > /var/www/html/cgi-bin/test2.htm")
        proc = subprocess.Popen(['sudo', 'echo', 'hello > /var/www/html/cgi-bin/test3.htm'])
        print(subprocess.check_output("/bin/echo hello > /var/www/html/cgi-bin/test2.htm", shell=True, timeout = 10))
    except:
        print("Unexpected error:", sys.exc_info())

    print(KILL_PROCESS)


def stopSensor():
    killProcess()
    os.spawnl(os.P_NOWAIT, STOP_PROCESS)


def restartProcess():
    killProcess()
    print(START_PROCESS)
    print(os.spawnl(os.P_NOWAIT, START_PROCESS))


def main():
    arguments = cgi.FieldStorage()
    for key in arguments.keys():
        value = arguments[key].value
        if key == 'action':
            if value == 'stop':
                stopSensor()
                print("ok")
                return
            elif value == 'start' or value == 'restart':
                restartProcess()
                print("ok")
                return
            elif value == 'resetdata':
                try:
                    with open(DATA_FILE, 'w') as outfile:
                        outfile.write('[]')
                except:
                    print("Unexpected error:", sys.exc_info())

                print("ok")
                return

    print("?")


main()

我解决了我的问题:http://alexanderhoughton.co.uk/blog/lighttpd-changing-default-user-raspberry-pi/