tags:

views:

234

answers:

3

I'm developing a web interface for an already existing desktop application. I've been looking for a way to allow the server to push content to the browser and ended up reaching Comet.

Navigating through the internet, and most of the questions here, I got answers like twisted, orbited, tornado and most of them even point to java applications like Jetty or StreamHub.

Without going too much deeper in this, I'd like to know is there's a chance to implement Comet-like communication using just standard lib modules like BaseHTTPServer and keep things as simple as possible as I don't need so much power and efficiency.

Note: Jython is a possibility, but I'd like to keep it with as less requirements as possible.

A: 

This is possible. Just don't close the connection to the client.

Georg
+4  A: 

As gs said, just keep the connection open.

Here's an example WSGI app that sends the current time to the client every second:

import time

def application(environ, start_response):
    start_response('200 OK', [('content-type', 'text/plain')])
    while True:
        time.sleep(1.0)
        yield time.ctime() + '\n'

if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    print "Serving on http://localhost:4000..."
    make_server('localhost', 4000, application).serve_forever()

If I go to the URL in my browser, I see this:

Mon Oct 05 12:21:26 2009
Mon Oct 05 12:21:27 2009
Mon Oct 05 12:21:28 2009
Mon Oct 05 12:21:29 2009
Mon Oct 05 12:21:30 2009
(...a new line appears every second...)

The problem with this approach is that you can't keep very many connections like this open at the same time. In fact, the wsgiref server is single-threaded, so you can only have one connection open at any time. If this is a problem then you must use a multithreaded (e.g. CherryPy) or non-blocking server (e.g. Twisted, Tornado, etc.).

lost-theory
Good enough. Thanks!
Santi
A: 

Extending what lost-theory has said, if you want to use comet for a passing messages between clients then you need to implement something like pubsub.

Using something like tornado for the pubsub is much simpler than with the single threaded wsgiref servers.