-1

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?

zx485
  • 28,498
  • 28
  • 50
  • 59
  • I changed your tag from _[tag:SSE]_ to _[tag:Server-Sent-Events]_, because they mean totally different things. – zx485 Aug 08 '19 at 18:30

2 Answers2

0

Simply in the exceptions make a new Response with the exception message.

return Response('Error, interface not found!')
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
  • Unfortunately, it didn't work as I wanted. The SSE does not stop and this message will be sent to the EventSource (if I put it in yield), I just want to print the error in the html page and stop the event. – Leandro Fabris Milani Aug 08 '19 at 15:17
0
from flask import Response

@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:
                # Close the session before the return statement
                conn.close_session()
                return Response('Error, interface not found!')

        except Exception as e:
            # Simply return the Response as shown in the above answer.
            # as posted by @Kostas Charitidis
            return Response('Error! ' + str(e))

    return Response(generate(), mimetype= 'text/event-stream')
Sammit
  • 169
  • 1
  • 8