2009-02-07 42 views
0

我有一個小問題在這裏:谷歌Apps的HTTP流與Python問題

前段時間我實現了HTTP流媒體使用PHP代碼,類似於在這個頁面上的內容:

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

我用非常類似的解決方案獲得數據。現在我嘗試從這個頁面使用第二個代碼(使用Python),但無論我做什麼,在一切完成後,我都會從python服務器接收responseText。下面是一些Python代碼:

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) 

這裏是JavaScript代碼:

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

任何人有知道我做錯了什麼?我怎麼能一個接一個地接收這些該死的消息,而不是在while循環結束時纔會收到這些消息?感謝您的幫助!

編輯:

我忘了補充主要的東西:服務器是谷歌應用程序服務器(我不知道的是,谷歌自己的實現),這裏是一些解釋的鏈接(我認爲侏儒):

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

+0

你使用的是什麼「python服務器」? – 2009-02-07 12:09:26

+0

是的,這是在這裏說的主要事情。那麼我更新了我的問題。 – Wilq32 2009-02-07 18:10:43

回答

1

這看起來像一個CGI代碼 - 我想象中的Web服務器緩衝來自CGI處理程序的響應。因此,選擇合適的工具並進行正確的配置確實是一個問題。

我建議使用wsgi服務器,並利用流支持wsgi有。

,這裏是你的示例代碼轉換爲WSGI應用:

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) 

有很多WSGI服務器,但這裏是與參考一個蟒蛇標準庫的例子:

from wsgiref.simple_server import make_server 

httpd = make_server('', 8000, app) 
httpd.serve_forever() 
+0

我使用谷歌應用服務器實現 - 你知道要處理這個? – Wilq32 2009-02-07 18:17:01