2012-07-17 22 views
0

我正在寻找一种方式来为一条路线发送多个响应。 问题是,从我读过的内容中我必须返回内容数据。 例如:Python为一条路线瓶装多个响应

@route('/events') 
def positions():  
    for i in xrange(5):   
     response.content_type = 'text/event-stream'   
     response.set_header('Cache-Control', 'no-cache')     
     now = datetime.datetime.now().time().replace(microsecond=0)   
     return "data: %s\n\n"%now 

有没有办法来取代一些函数调用的最后一行,这样我就可以把所有的答复,然后退出路线?

谢谢,
奥默尔。

+1

你不能那样做。一个HTTP请求只能有一个HTTP响应。 – 2012-07-17 09:13:35

回答

0

我不是100%确定我明白你在问什么,所以我可能没有正确回答,但是这会做你想做的吗?

@route('/events') 
def positions(): 
    output = '' 
    for i in xrange(5): 
     now = datetime.datetime.now().time().replace(microsecond=0) 
     output += "%s\n\n"%now 
    response.content_type = 'text/event-stream'     
    response.set_header('Cache-Control', 'no-cache') 
    return "data: " + output