Python Bottle SSE
Question: I'm trying to get Server Sent Events to work from Python, so I found a little
demo code and to my surprise, it only partly works and I can't figure out why.
I got the code from [here](https://gist.github.com/werediver/4358735) and put
in just a couple little changes so I could see what was working (I included a
print statement, an import statement which they clearly forgot, and cleaned up
their HTML to something I could read a little easier). It now looks like this:

    
    
    # Bottle requires gevent.monkey.patch_all() even if you don't like it.
    from gevent import monkey; monkey.patch_all()
    from gevent import sleep
    
    from bottle import get, post, request, response
    from bottle import GeventServer, run
    import time
    
    
    sse_test_page = """
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js "></script>
            <script>
                var es = new EventSource("/stream");
                es.onmessage = function(e) {
                    document.getElementById("log").innerHTML = e.data;
                }
            </script>
        </head>
        <body>
            <h1>Server Sent Events Demo</h1>
            <p id="log">Response Area</p>
        </body>
    </html>
    """
    
    
    @get('/')
    def index():
        return sse_test_page
    
    
    @get('/stream')
    def stream():
        # "Using server-sent events"
        # https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events
        # "Stream updates with server-sent events"
        # http://www.html5rocks.com/en/tutorials/eventsource/basics/
    
        response.content_type  = 'text/event-stream'
        response.cache_control = 'no-cache'
    
        # Set client-side auto-reconnect timeout, ms.
        yield 'retry: 100\n\n'
    
        n = 1
    
        # Keep connection alive no more then... (s)
        end = time.time() + 60
        while time.time() < end:
            yield 'data: %i\n\n' % n
            print n
            n += 1
            sleep(1)
    
    
    if __name__ == '__main__':
        run(server=GeventServer, port = 21000)
    

So here's what ends up happening: I can see the original header and paragraph
on the website, but response area never changes. On the python side, it prints
`n` once per second, but I never see that change on the web page. I get the
feeling that I just lack a fundamental understanding of what I'm trying to do
but I can't find anything missing.

I'm running Python 2.7, windows 7, chrome 43.0.2357.81 m.

**EDIT:** I got rid of the extra quotation mark. Now it only seems to update
when it gets to 60 (which I guess is better than not at all...) Why would it
wait until the end of the function to send the event?


Answer: You've got 2 sets of quotes after p id="log""

