如何在python中组合flask和请求?

2024-10-01 22:34:19 发布

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

internallogin页面上,如果用户进行了身份验证,我想发出一个post请求。为此,我有以下代码。在

@app.route('/internallogin', methods=['POST', "GET"])
def showInternallogin():
    uname = request.form.get("name")
    passw = request.form.get("pass")
    if is_valid_login(uname, passw):
        print("legal_login")
        req =  requests.Request(method="POST", url='http://localhost:5000/internal', data={"name": uname, "passw": passw})
        return req
    else:
        return redirect('/login')

在打印“合法登录”之后,马上发生的是我得到了一个错误TypeError: 'Request' object is not callable。如何使用烧瓶发出邮寄请求?在


Tags: 用户nameformgetreturnisrequestlogin
2条回答

您可以使用^{}发布帖子,如下所示:

response = requests.post('http://localhost:5000/internal', data={...})

但是,通常不需要从服务器本身调用服务器。您应该考虑将/internal路由中的逻辑抽象出来,并在该路由中直接调用它。在

以下是来自@Karin的回答

response = requests.post('http://localhost:5000/internal', data={...})

以下可能是您收到错误的原因。在

{a1}

If a tuple is returned the items in the tuple can provide extra information. Such tuples have to be in the form (response, status, headers) where at least one item has to be in the tuple. The status value will override the status code and headers can be a list or dictionary of additional header values.

这可能会解决这个问题。在

^{pr2}$

相关问题 更多 >

    热门问题