This is my first application using Flask and SSE, I need to find a way to return to the page some exceptions that could happen during the execution of the event. This application uses Netconf to get some realtime information from a router.
I've created some exceptions that only show me in the console of Flask the messages, but I need to it show in the HTML interface and interrupt the stream.
@app.route('/data')
def data():
def generate():
try:
conn = manager.connect(
host='10.0.0.1',
port='22',
username='admin',
password='123',
timeout=10,
device_params={'name':'junos'},
hostkey_verify=False
def getInterface(command):
interface = command.xpath('//system-information')
return interface
try:
interface = getInterface(conn.command('code to get information'))
except:
print('Error, interface not found!')
conn.close_session()
except Exception as e:
print('Error! ' + str(e))
return Response(generate(), mimetype= 'text/event-stream')
This code above if gets an exception it doesn't stop the SSE 'looping' and keep logging into the console repeatedly the message "Error, interface not found!"
How can I handle it by sending these exceptions to the HTML interface and stop the execution of SSE when it happens?