简体   繁体   English

启动和停止进程的最佳 Python Web 框架

[英]Best python web framework to start and stop a process

I have a python script that is kicked off via command line and can be killed at any time.我有一个通过命令行启动的 python 脚本,可以随时终止。

I would like to create a simple web interface where a user can login and start the process and kill it on command.我想创建一个简单的 Web 界面,用户可以在其中登录并启动进程并根据命令将其终止。

I realize this is probably possible with Django and a library such as background tasks, but that seems a little overkill for something as simple as what I'm trying to do.我意识到这对于 Django 和诸如后台任务之类的库来说可能是可能的,但是对于像我想要做的这样简单的事情来说,这似乎有点矫枉过正。

Is there a simple way to start and stop a python job via a web page?有没有一种通过网页启动和停止 python 作业的简单方法?

This isn't Python and I'm not sure how complex you want to get, but it is something I use quite often and it is very simple and bare bones.这不是 Python,我不确定你想要多复杂,但它是我经常使用的东西,它非常简单和裸露。 Just a little Go program which starts a server and on receiving a request it runs the dowebhook.sh command.只是一个小的 Go 程序,它启动服务器并在收到请求时运行dowebhook.sh命令。 You can swap out whatever command you like and can even add more routes with more handlers to run more commands.你可以换掉你喜欢的任何命令,甚至可以添加更多的路由和更多的处理程序来运行更多的命令。 You'll have to compile the program for the architecture you're running it on, but the pro is that you now have a binary and there's no need for a framework or dependencies.您必须为运行它的架构编译程序,但优点是您现在拥有一个二进制文件,并且不需要框架或依赖项。 Keep the program running using either supervisor or systemd.使用 supervisor 或 systemd 保持程序运行。 Set up a proxy to :8000 in nginx (or whatever server you like) to define the endpoint.在 nginx(或您喜欢的任何服务器)中设置一个到 :8000 的代理来定义端点。 Done.完毕。
Now your command executes with just a call to the endpoint, which you can trigger any way you like - if you're talking interface it can be a static page with a button that makes an AJAX call.现在您的命令只需调用端点即可执行,您可以以任何您喜欢的方式触发它 - 如果您正在谈论界面,它可以是一个带有按钮的静态页面,可以进行 AJAX 调用。

package main

import (
    "fmt"
    "net/http"
    "os/exec"
)

func handler(w http.ResponseWriter, r *http.Request) {
    out, err := exec.Command("dowebhook.sh").Output()
    if err != nil {
        fmt.Printf("There was a webhook error: %s", err)
    }
    fmt.Fprintf(w, "Result of %s is %s", r.URL.Path[1:], out)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8000", nil)
}

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

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