2017-05-31 147 views
0

我打電話的燒瓶API,服務於舊的迴應gunicorn運行和供應出來nginx的所有在Linux中viretal ENVAPI在燒瓶

當我使用捲曲或郵遞員來測試我得到1每次我運行4個響應 - 但主要是響應2下面

這裏是我的瓶api py文件。我是新來這個,所以藉口任何錯誤:當我連續運行下面幾次測試

app = Flask(__name__) 
now = datetime.now() 
timestamp=str(now.strftime("%Y-%m-%d %H:%M")) 

# assignes route for POSTING JSON requests 
@app.route('/api/v1.0/my-api', methods=['POST']) 
#@requires_auth 
def runscope(): 
    if request.method == 'POST': 
     in_json = request.json 
    in_json["api_datetime"] = timestamp 
    json_to_file(in_json) 
     return timestamp + "New msg log file success!" 


# assigns route for the default GET request 
@app.route('/') 
def index(): 
    return 'test on the index' 


if __name__ == "__main__": 
    app.debug = True 
    application.run() 

# function to drop request data to file 
def json_to_file(runscope_json): 
    with open('data/data.json', 'a') as outfile: 
      json.dump(runscope_json, outfile, indent=2) 

所以

curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost:8000/api/v1.0/my-api 

我得到任何 1.響應:「新信息的日誌文件成功!」使用JSON獲取到文件I指定

OR

  • 響應: 「!日誌文件的成功」這是在上面的Python代碼的舊版本!數據獲取到該文件,但沒有時間戳作爲舊的代碼沒有它
  • OR

  • 「未找到該請求的URL沒有在發現服務器,如果你手動輸入URL,請檢查你的拼寫,然後重試。「
  • OR

  • { "message": "Authenticate." }這是我在代碼的另一箇舊版本的響應!
  • 注:我做了一個「gunicorn my-api:app」和nginx重啓,如果我改變了代碼,並確保手動刪除.pyc文件第一

    誰能幫幫忙?它從哪裏得到舊的代碼響應?爲什麼它是間歇性的,有時只給我預期的新代碼響應?

    回答

    1

    您是否嘗試將緩存相關的標題添加到您的回覆中?喜歡的東西:

    # example code 
    @app.route('/api/v1.0/my-api', methods=['POST']) 
    def runscope(): 
        response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' 
        response.headers['Pragma'] = 'no-cache' 
        # rest of the code... 
    

    特定瓶路線...

    或者,如果你想要爲所有請求禁用緩存:

    # example code 
    @app.after_request 
    def add_header(response): 
        response.cache_control.max_age = 60 
        if 'Cache-Control' not in response.headers: 
         response.headers['Cache-Control'] = 'no-store' 
        return response 
    
    +0

    它似乎像一個緩存的問題 - 我想這些標題,但我得到了這個錯誤response.headers ['緩存控制'] ='沒有緩存,無存儲,必須重新驗證' NameError:全球名稱「響應」未定義 – user3760188

    +0

    啊,是的,這只是一個例子代碼......你應該專門爲你的用例處理它......我用我的答案更新了我的答案礦石通用解決方案太... – errata

    +0

    謝謝。我嘗試了緩存修復的各種變體,但是遇到了一堵牆,於是我去了並定義了一條新路線「my-api1」,並解決了「舊代碼」問題。所以我現在得到正確的答覆。目前這是一種可行的解決方法。 – user3760188