Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 476
hh54188 Avatar asked Jul 22 '13 11:07

hh54188


People also ask

How do you capture an exception in Python?

Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.

Can we do error handling in Flask project?

Flask is no exception to allowing for custom errors. Not only can we handle things like HTTP 500, or 404 errors, but we can still also use the typical try/except syntax to handle other errors logically.

How do you show error messages in Flask?

Returning API errors as JSON When using Flask for web APIs, you can use the same techniques as above to return JSON responses to API errors. abort() is called with a description parameter. The errorhandler() will use that as the JSON error message, and set the status code to 404.


1 Answers

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.

like image 120
Nop Avatar answered Oct 04 '22 10:10

Nop