从vi中的url获取变量

2024-03-29 09:04:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想向ip:port/this1234发出一个httpget请求,并在Python代码中使用“1234”作为变量。“1234”是一个任意的int。如何将int作为我的视图的参数?你知道吗

@app.route('/stop####')
    def stop####():
        global stopped
            if stopped:
               call(["./stop.sh"], shell = True)
            stopped = False
        return "started"
    return "already started"

Tags: 代码ip视图app参数returnportdef
2条回答

你会想看看Quickstart guide。你知道吗

同时,您可以使用

myvar =  request.form["myvar"]

开始使用

myvar = request.args.get("myvar")

指南接着提到了一些错误处理建议,并更深入地引用了request object page。你知道吗

We recommend accessing URL parameters with get or by catching the KeyError because users might change the URL and presenting them a 400 bad request page in that case is not user friendly.

For a full list of methods and attributes of the request object, head over to the request documentation.

您可能还想看一下routing。我不确定你想在你的路线上用英镑符号来完成什么(编辑:我明白你重读的意思;见底部的编辑)。请注意下面来自SitePoint上comment的引用。你知道吗

browsers don't send the #whatever part of the URL to the server in the HTTP request when requesting the page

好的,如果您想在URI中传递值,我建议使用类似于:example.com/this/1234的方法,并且您的路由规则看起来像是@app.route('/this/<myVar>')在您的def my_func(myVar):之上

最后,在某种程度上,基于http请求杀死任何进程似乎是非常大胆的,但是您最了解您的环境,而且据我所知,它甚至可能不会暴露在internet上。祝你好运,别忘了注意安全。你知道吗

我认为最明显的方法是:

@app.route('/kill/<int:pid>')
def kill(pid):
    .....

相关问题 更多 >