views:

1757

answers:

2

I got a little question here:

Some time ago I implemented HTTP Streaming using PHP code, something similar to what is on this page:

http://my.opera.com/WebApplications/blog/show.dml/438711#comments

And I get data with very similar solution. Now I tried to use second code from this page (in Python), but no matter what I do, I receive responseText from python server after everything completes. Here are some python code:

print "Content-Type: application/x-www-form-urlencoded\n\n"

i=1
while i<4:
 print("Event: server-time<br>")
 print("data: %f<br>" % (time.time(),))
 sys.stdout.flush()
 i=i+1
 time.sleep(1)

And here is Javascript Code:

ask = new XMLHttpRequest();
ask.open("GET","/Chat",true);
setInterval(function()
{
if (ask.responseText) document.write(ask.responseText);
},200);
ask.send(null);

Anyone got idea what I do wrong ? How can I receive those damn messages one after another, not just all of them at the end of while loop? Thanks for any help here!

Edit:

Main thing I forgot to add: server is google app server (i'm not sure is that google own implementation), here is a link with some explanation (i think uhh):

http://code.google.com/intl/pl-PL/appengine/docs/python/gettingstarted/devenvironment.html http://code.google.com/intl/pl-PL/appengine/docs/whatisgoogleappengine.html

+1  A: 

That looks like a cgi code - I imagine the web server buffers the response from the cgi handlers. So it's really a matter of picking the right tools and making the right configuration.

I suggest using a wsgi server and take advantage of the streaming support wsgi has.

Here's your sample code translated to a wsgi app:

def app(environ, start_response):
    start_response('200 OK', [('Content-type','application/x-www-form-urlencoded')])
    i=1
    while i<4:
        yield "Event: server-time<br>"
        yield "data: %f<br>" % (time.time(),)
        i=i+1
        time.sleep(1)

There are plenty of wsgi servers but here's an example with the reference one from python std lib:

from wsgiref.simple_server import make_server

httpd = make_server('', 8000, app)
httpd.serve_forever()
ionelmc
I use google apps server implementation - do you know to handle this?
Wilq32
+3  A: 

Its highly likely App Engine buffers output. A quick search found this: http://code.google.com/appengine/docs/python/tools/webapp/buildingtheresponse.html

The out stream buffers all output in memory, then sends the final output when the handler exits. webapp does not support streaming data to the client.

Richard Levasseur
that sux ;( ok then i try it diffrent way :)
Wilq32