我设置了 lighttpd 服务器以及 webpy 和 fastcgi。我试图在每次访问 wenpy 应用程序时简单地运行 python 脚本。尽管看起来即使我给出正常的 python 代码来执行脚本,它也什么也没做。所以我希望能够运行这个脚本,任何想法都会有帮助。
#!/usr/bin/env python
import web, os
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
os.system("python /srv/http/script/script.py")
if not name:
name = 'world'
return "Running"
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
if __name__ == "__main__":
app.run()
假设您的方法运行,我最关心的是发生错误,并且您没有获得解释问题的标准输出(os.system 将获得返回值,例如退出代码)。 Python 文档建议将其替换为子进程,我喜欢这样做:
from subprocess import Popen, PIPE
proc = Popen('ls', shell=True, stdout=PIPE)
proc.wait()
proc.communicate()
我最终发现将脚本作为 webpy 应用程序的对象调用效果很好,但在外部执行它根本无法工作。
它不起作用的原因可能是lighttpd是一个守护进程,守护进程关闭它们的stdin/stdout/stderr文件描述符。您运行的程序需要一个打开了这些描述符的终端,但它们是从调用者继承的,因此是关闭的。因此,当调用外部程序时,您应该自己提供它们。例如:
from subprocess import call, STDOUT, PIPE
retval = call(['program', 'arg1', 'arg2'], stdin = PIPE, stdout = PIPE, stderr = STDOUT)
请参阅 Python 文档
中的解释和示例您要查看的操作结果是:
print proc.stdout.read()
在您发出
Popen
命令后
$ apt 更新 && apt 升级 -y $ apt install -y python git $ git clone https://github.com/A-KTO-Tbl/INSIDE && pip install -r INSIDE/Core/requirements.txt