简体   繁体   English

如何在Flask上捕获这样的异常?

[英]How to catch an Exception like this on 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? try except抓住它,我怎么能用try except

I recently ran into this error message while trying to use Flask to serve audio files. 我最近在尝试使用Flask来提供音频文件时遇到了此错误消息。 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. 此错误消息不是来自Flask应用程序,而是来自用于分派请求数据的基础SocketServer。 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. 发生的事情是由于某种原因,与客户端的连接正在结束,但Flask继续尝试将数据写入已关闭的套接字。 You can't catch this exception from your Flask application, because Flask catches it for you. 您无法从Flask应用程序中捕获此异常,因为Flask会为您捕获它。 Flask prints it out as a service to you, notifying you that the stream was closed prematurely, ie before Flask finished writing data to the stream. Flask将其作为一项服务打印出来,通知您流已过早关闭,即在Flask完成向数据流写入数据之前。

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. 总而言之,此错误消息是Flask的内部信息,Flask正在打印它,告诉您在连接关闭之前无法将所有数据传递给客户端。 You can't catch it, and you shouldn't have any reason to catch it. 你无法抓住它,你没有任何理由去捕捉它。

I've found this solution to be a good at least temporary fix. 我发现这个解决方案至少可以暂时修复。

if __name__ == '__main__':
  while True:
    try:
       app.run(use_reloader = False)
    except:
      pass

You can add your own exit logic, or leave the program with CTRL + \\ which sends SIGQUIT. 您可以添加自己的退出逻辑,或者使用CTRL + \\保留程序,发送SIGQUIT。 (important if you're running threaded flask) (如果你正在运行带螺纹的烧瓶,这很重要)

You however can't: 但是你不能:

   except KeyboardInterupt:

Because Flask already catches KeyboardInterupt exceptions and handles them. 因为Flask已经捕获了KeyboardInterupt异常并处理它们。

error 10052 means you're using windows, so as far as I know, close the command window to exit the program 错误10052表示您正在使用Windows,因此据我所知,关闭命令窗口以退出程序

It is probably due to the port number being used which is 54481 by looking at your error message. 这可能是由于使用的端口号是54481,通过查看您的错误消息。 It might be clashing with something else. 它可能与其他东西发生冲突。 I also suggest not to use the use_reloader parameter since your DEBUG is already set to False. 我还建议不要使用use_reloader参数,因为你的DEBUG已经设置为False。 So flask will not reload any code changes. 因此,烧瓶不会重新加载任何代码更改。 Can you instead do this : 你可以这样做:

if __name__ == '__main__':
    app.run(port=5000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM