如何在Flask上捕获这样的异常? [英] How to catch an Exception like this on Flask?

查看:1281
本文介绍了如何在Flask上捕获这样的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  from flask import Flask 

app = Flask(__ name__)

@ app.route('/')
def welcome():
returnOK


app.config.update(
DEBUG = True


if __name__ =='__main__':
app.run(use_reloader = False)

当我运行它并访问它时,有时(并不总是)它不能响应请求并抛出一个除外:
$ b $ pre $ 在处理来自('127.0.0.1',54481)的请求期间发生异常
Traceback(最近一次调用last ):
文件c:\python27\Lib\SocketServer.py,行295,在_handle_request_noblock
self.process_request(request,client_address)
文件c:\ python27 \Lib\SocketServer.py,第321行,在process_request
self.finish_request(request,client_address)
文件c:\python27\Lib\SocketServer.py,行334,in finish_request
self.RequestHandlerClass(request,client_address,self)
文件c:\python27\Lib\SocketServer.py,行651,在__init__
self.finish()
文件c:\python27\Lib\SocketServer.py,行710,完成
self.wfile.close()
文件c:\python27\在$ b $ self.flush()
文件c:\python27\Lib\socket.py中,第303行,在
中的文件Lib\socket.py $ b self._sock.sendall(view [write_offset:write_offset + buffer_size])
error:[Errno 10053]

我不明白是什么原因造成这个故障?我怎样才能解决它?



以及如何使用 try除了来捕捉它?

解决方案

我最近在尝试使用Flask来提供音频文件时遇到了这个错误信息。我得到这个错误消息,当客户端在流结束之前关闭流。



这个错误信息不是来自您的Flask应用程序,而是来自底层用于调度请求数据的SocketServer。发生什么事情是由于某种原因客户端的连接正在结束,但Flask继续尝试将数据写入关闭的套接字。您不能从Flask应用程序中捕获此异常,因为Flask会为您捕获该异常。 Flask打印出来作为一项服务给你,通知你,这个流被过早关闭了,也就是在Flask完成写入数据到流之前。



总之,这个错误信息在Flask内部,Flask正在打印它,告诉你在连接关闭之前无法将所有数据传送到客户端。你不能抓住它,你应该没有任何理由赶上它。


I run a simple flask app like this:

from flask import Flask

app = Flask(__name__) 

@app.route('/')
def welcome():
    return "OK"


app.config.update(
    DEBUG = True
)

if __name__ == '__main__':
    app.run(use_reloader = False)

when I run it and visit it, sometimes(not always) it could't response the request and throw an except:

Exception happened during processing of request from ('127.0.0.1', 54481)
Traceback (most recent call last):
  File "c:\python27\Lib\SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "c:\python27\Lib\SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "c:\python27\Lib\SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "c:\python27\Lib\SocketServer.py", line 651, in __init__
    self.finish()
  File "c:\python27\Lib\SocketServer.py", line 710, in finish
    self.wfile.close()
  File "c:\python27\Lib\socket.py", line 279, in close
    self.flush()
  File "c:\python27\Lib\socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053]

I can't understand what cause this fault? and how can I solve it?

and how can I use try except to catch it?

解决方案

I recently ran into this error message while trying to use Flask to serve audio files. I get this error message whenever the client closes the stream before the end of the stream.

This error message doesn't originate from your Flask application, but rather from the underlying SocketServer used to dispatch request data. What is happening is the connection to the client is ending for some reason, but Flask continues to try to write data to the closed socket. You can't catch this exception from your Flask application, because Flask catches it for you. Flask prints it out as a service to you, notifying you that the stream was closed prematurely, i.e. before Flask finished writing data to the stream.

To sum it up, this error message is internal to Flask, Flask is printing it to tell you that it couldn't get all the data to the client before the connection closed. You can't catch it, and you shouldn't have any reason to catch it.

这篇关于如何在Flask上捕获这样的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆