How to catch an exception like this on a bulb?

I run a simple flack application:

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 cannot respond to the request and throw with the exception of:

 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 do not understand what is the cause of this error? and how can i solve it?

and how can I use try except to catch it?

+7
python flask try-catch
source share
3 answers

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

This error message does not come from the Flask application, but rather from the underlying SocketServer used to send the request data. The fact is that the connection to the client for some reason ends, but Flask continues to try to write data to the private socket. You cannot catch this exception from your Flask application, because Flask catches it. Flask prints it as a service for you, notifying you that the stream has been closed ahead of schedule, i.e. Before Flask finished writing data to the stream.

To summarize, this error message is internal to Flask, Flask prints it to inform you that it was not able to get all the data to the client before closing the connection. You cannot catch him, and you should have no reason to catch him.

+10
source share

I 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. (important if you are using a threaded flask)

However, you cannot:

  except KeyboardInterupt: 

Because the flag already catches KeyboardInterupt exceptions and handles them.

error 10052 means that you are using windows, as far as I know, close the command window to exit the program

0
source share

This is probably due to the fact that the port number is used, which is 54481, looking at your error message. Perhaps he will run into something else. I also suggest not using the use_reloader parameter, since your DEBUG is already set to False. Thus, the bulb does not reload any code changes. You can do it:

 if __name__ == '__main__': app.run(port=5000) 
-3
source share

All Articles