Flask Json请求400记录?

Spa*_*ide 2 python flask

我编写了自己的自定义客户端,通过我的wifi卡将原始http请求发送到我的烧瓶网络服务器.

这就是典型的请求:

Content-Length: 214
User-Agent: blah
Connection: close
Host: 1.2.3.4:5000
Content-Type: application/json

{"data":[{"scoutId":2,"message":"ph=5.65"},{"scoutId":4,"message":"ph=4.28"},{"scoutId":3,"message":"ph=4.28"},{"scoutId":2,"message":"ph=5.65"},{"scoutId":4,"message":"ph=4.28"},{"scoutId":3,"message":"ph=4.30"}]}
Run Code Online (Sandbox Code Playgroud)

有时,我的客户搞砸了并将错误的JSON请求发送到我的烧瓶服务器.通常,烧瓶只会显示:

1.2.3.5 - - [01/Sep/2014 22:13:03] "POST / HTTP/1.1" 400 -
Run Code Online (Sandbox Code Playgroud)

并没有提供有关请求的信息.

我想跟踪在我的环境中产生400的每个请求,并分析导致这些错误的原因.

我在哪里可以将自定义错误功能放在我的烧瓶服务器中?

Ste*_*lla 5

尝试启用此功能:

app.config['TRAP_BAD_REQUEST_ERRORS'] = True
Run Code Online (Sandbox Code Playgroud)

这应该使得flask引发异常而不是仅记录400(参见此处的文档).

如果您需要做更多的事情,请创建一个事件处理程序:

http://flask.pocoo.org/docs/0.10/patterns/errorpages/

@app.errorhandler(400)
def page_not_found(exc):
    #do something with the exception object `exc` here
    ....
Run Code Online (Sandbox Code Playgroud)

或者尝试在try/except中包装视图函数的主体.