【发布时间】:2017-11-01 07:33:20
【问题描述】:
我正在调用一个 Flask api,由 gunicorn 运行,由 nginx 提供,全部在 linux box viretal env 上
当我使用 curl 或 postman 进行测试时,我每次运行时都会随机收到 4 个响应中的 1 个 - 但主要是下面的响应 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 获取我指定的文件
或
- 响应:“日志文件成功!”这是上面python代码的旧版本!数据到达文件但没有时间戳,因为旧代码没有它
或
- “未找到服务器上未找到请求的 URL。如果您手动输入了 URL,请检查拼写并重试。”
或
-
{ "message": "Authenticate." }这是我在另一个旧版本代码中的响应!
注意:如果我更改了代码,我会执行“gunicorn my-api:app”和nginx 重新启动,确保首先手动删除.pyc 文件
有人可以帮忙吗?它从哪里获得旧代码响应?为什么它是断断续续的,只是有时给我预期的新代码响应?
【问题讨论】:
标签: python nginx gunicorn flask-restful