2014-10-30 45 views
0

我在我的應用程序中有一個端點,當它打到時我想向客戶端發送一個響應。我在燒瓶裏這樣做。如何在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

你應該正確地格式化你的代碼!縮進不匹配。 – 2014-10-30 13:18:49

+0

我使用make_response修復了它。謝謝 – station 2014-10-30 13:21:27

回答

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

相關問題