简体   繁体   English

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

[英]How do I write text to a response in Flask

I have a endpoint in my application which when hit I want to send a response to the client. 我的应用程序中有一个端点,当命中时,我想向客户端发送响应。 I am doing it like this in 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)

This is throwing a error that call takes three arguments. 这将引发一个错误,该调用需要三个参数。 What am I missing here ? 我在这里想念什么?

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

then access "localhost:5000/" it will be ok. 然后访问“ localhost:5000 /”就可以了。

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

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