2017-05-31 148 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