如何在Flask中为响应写入文本

时间:2014-10-30 13:15:42

标签: python flask

我的应用程序中有一个端点,当命中时我想向客户端发送响应。我在烧瓶中这样做。

from flask import Response
def heartbeat():
response = Response()
response.headers['Last-Modified'] = datetime.now()
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Expires'] = '-1'
return response(heartbeat)

这会抛出一个错误,调用需要三个参数。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

from flask import Response, Flask

app = Flask(__name__)

@app.route("/")
def heartbeat():
    response = Response()
    response.headers['Last-Modified'] = datetime.now()
    response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
    response.headers['Expires'] = '-1'
    return response

然后访问“localhost:5000 /”就可以了。