【问题标题】:Python Bottle multiple responses for one route一条路线的Python Bottle多个响应
【发布时间】:2012-08-02 07:47:41
【问题描述】:

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

@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

有没有办法替换某些函数调用中的最后一行,以便我可以发送所有响应然后退出路由?

谢谢,
奥马尔。

【问题讨论】:

  • 你不能那样做。一个 HTTP 请求只能有一个 HTTP 响应。

标签: python bottle


【解决方案1】:

我不能 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

【讨论】:

    猜你喜欢
    • 2021-03-05
    • 1970-01-01
    • 2011-11-17
    • 2015-02-23
    • 2013-11-23
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多