簡體   English   中英

如何在Flask中將文本寫入響應

[英]How do I write text to a response 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)

這將引發一個錯誤,該調用需要三個參數。 我在這里想念什么?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM