[英]Easiest way to run python script with parameters on a web server
Thought this would be straightforward, but it's giving me a lot of trouble. 认为这很简单,但它给了我很多麻烦。 What's the simplest way to run a python script with params on a web server?
在Web服务器上使用params运行python脚本的最简单方法是什么? I've tried Node, but had a lot of trouble with spawning child processes, tried PHP but ran into a similar problem and just couldn't get
exec
to work for even the simplest script. 我已经尝试过Node,但是在生成子进程时遇到了很多麻烦,尝试了PHP但是遇到了类似的问题,即使是最简单的脚本也无法让
exec
工作。 Just spent a while trying Django, but that turned out to be far too involved. 刚刚花了一段时间尝试Django,但事实证明它过于牵扯。 How can I do this without re-inventing the wheel?
如何在不重新发明轮子的情况下做到这一点?
For example, take the following request: 例如,请执行以下请求:
www.example.com/someprogram?foo=bar&bar=baz
And render the output from a script run with those params: 并使用这些参数运行脚本运行的输出:
python someprogram.py foo bar
Try bottle , a micro web framework: 试试瓶子 ,一个微型网络框架:
someprogram.py: someprogram.py:
def f(foo, bar):
return "foo: {}, bar: {}"format(foo, bar)
server.py: server.py:
from bottle import route, run
import someprogram
@route('/')
def home():
return someprogram.f(request.query.foo, request.query.bar)
if __name__ == "__main__":
run(debug=True, reloader=True)
To run: 跑步:
pip install bottle
python server.py
Use subprocess.check_output()
to spawn a process if you don't want to import someprogram.py
. 如果您不想导入
someprogram.py
subprocess.check_output()
生成进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.