简体   繁体   English

如何使用http.server执行服务器端python脚本?

[英]How do you execute a server-side python script using http.server?

I have a collection of python scripts, that I would like to be able to execute with a button press, from a web browser. 我有一个python脚本集合,我希望能够通过Web浏览器按一下按钮即可执行。

Currently, I run python -m http.server 8000 to start a server on port 8000. It serves up html pages well, but that's about all it does. 当前,我运行python -m http.server 8000在端口python -m http.server 8000上启动服务器。它可以很好地提供html页面,但这就是它的全部工作。 Is it possible to have it execute a python script (via ajax) and return the output, instead of just returning the full text of the .py file. 是否可以让它执行python脚本(通过ajax)并返回输出,而不是仅返回.py文件的全文。

Additionally, if not, is there a simple (as in only 1 or 2 files) way to make this work? 另外,如果没有,是否有一种简单的方法(仅使用1个或2个文件)来完成这项工作? I'm looking for the equivalent of PHP -s, but for python. 我正在寻找与PHP -s相当的产品,但对于python。

For completeness, this is my html 为了完整性,这是我的html

<h1>Hello World</h1>

<button>
Click me!
</button>

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.js">    </script>

<script>
  $('button').click(function(){
    $.get('/gui/run_bash.py');
  });
</script>

http.server merely serves static files, it does not do any serverside processing or execute any code when you hit a python file. http.server只提供静态文件,当您点击python文件时,它不执行任何服务器端处理或执行任何代码。 If you want to run some python code, you'll have to write an application to do that. 如果要运行一些python代码,则必须编写一个应用程序来执行此操作。 Flask is a Python web framework that is probably well-suited to this task. Flask是一个Python Web框架,很可能适合此任务。

Your flask application might look something like this for executing scripts... 您的flask应用程序可能看起来像这样执行脚本...

import subprocess
from flask import Flask
app = Flask(__name__)

SCRIPTS_ROOT = '/path/to/script_dir' 
@app.route('/run/<script_name>')
def run_script(script_name):
    fp = os.path.join(SCRIPTS_ROOT, script_name)
    try:
        output = subprocess.check_output(['python', fp])
    except subprocess.CalledProcessError as call:
        output = call.output # if exit code was non-zero
    return output.encode('utf-8') # or your system encoding

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8000)

And of course, I should include an obligatory warning 'having a webserver execute commands like this is insecure', etc, etc. Check out the Flask quickstart for more details. 当然,我应该包括强制性警告“让网络服务器执行不安全的命令”等,等等。有关更多详细信息,请查看Flask快速入门

Add --cgi to your command line. --cgi添加到命令行。

python -m http.server --cgi 8000

Then place your python scripts in ./cgi-bin and mark them as executable. 然后将您的python脚本放在./cgi-bin中,并将其标记为可执行文件。

$ mkdir cgi-bin
$ cp hello.py cgi-bin/hello.py
$ chmod +x cgi-bin/hello.py

You may need to slightly modify your python scripts to support the CGI protocol. 您可能需要稍微修改python脚本以支持CGI协议。

Here is the server running: 这是服务器正在运行:

$ cat cgi-bin/hello.py 
#! /usr/bin/env python3

print("Content-Type: application/json")
print()

print('{"hello": "world"}')
radams@wombat:/tmp/z/h$ python -m http.server --cgi
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [20/Mar/2018 18:04:16] "GET /cgi-bin/hello.py HTTP/1.1" 200 -

Reference: https://docs.python.org/3/library/http.server.html#http.server.CGIHTTPRequestHandler 参考: https : //docs.python.org/3/library/http.server.html#http.server.CGIHTTPRequestHandler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM