gpt4 book ai didi

python - 从 webpy 运行 python 脚本

转载 作者:行者123 更新时间:2023-11-28 16:54:25 25 4
gpt4 key购买 nike

我设置了一个 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()

关于python - 从 webpy 运行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2352870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com