问题在于:subprocess.call("python /home/pi/test.py")
它从名为“python/home/pi”的可执行文件中查看您的PATH/测试.py“-所有一个程序,名称中有空格。您需要传递一个参数列表,而不是命令行。如the docs解释:args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.
所以,这样做:
^{pr2}$
它将查找名为“python”的可执行文件,并使用“/home/pi”运行它/测试.py”作为它的第一个参数(并且以“python”作为其第零个参数),这就是您想要的。在
它可能仍然不起作用-python必须位于您的特殊Apache用户的路径上,通过一个可访问的路径,并且是可执行的。通常通过sys.executable或{}或任何合适的方法比较好。在
同时,如果您不直接调用call并忽略结果,那么调试它会更容易(比如在并非完全不可能的情况下)。例如:ret = call(["python", "/home/pi/test.py"])
if ret:
print "Running test.py returned", ret
……或者try:
check_call(["python", "/home/pi/test.py"])
except Exception as e:
print "Tried to run test.py, got back", repr(e)
或者…你真的希望它的输出直接传递到CGI吗?如果没有,您可能需要捕获它的输出并对其执行某些操作,例如:try:
output = check_output(["python", "/home/pi/test.py"])
except Exception as e:
print "Tried to run test.py, got back", repr(e)
else:
print "test.py said", output