Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将文本写入Flask中的响应_Python_Flask - Fatal编程技术网

Python 如何将文本写入Flask中的响应

Python 如何将文本写入Flask中的响应,python,flask,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-c

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

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)
这将引发一个错误,该调用包含三个参数。我错过了什么

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/就可以了。

您应该正确格式化代码!缩进不匹配。我使用make_response修复了它。谢谢