将 for 循环与 bottle 一起使用

using for loops with bottle

我目前正在开发一个基于 bottle 的 python 网络应用程序,所以我想做的是打印 for 循环的结果 我已经尝试了以下

#!/usr/bin/python
from bottle import *
@route('/')
def index():
    for i in range(10):
         return i

但这没有用,我从开发服务器输出中得到了这个

localhost - - [13/Jan/2017 18:11:38] "GET /request HTTP/1.1" 200 0
localhost - - [13/Jan/2017 18:11:40] "GET /favicon.ico HTTP/1.1" 200 0

所以我尝试了这个

#!/usr/bin/python
from bottle import *
@route('/')
def index():
    sumOfValues=0
    for i in range(10):
        sumOfValues+=i
    return sumOfValues

这也没有用 我的开发服务器给了我这个

localhost - - [13/Jan/2017 18:15:44] "GET /request HTTP/1.1" 500 746
localhost - - [13/Jan/2017 18:15:46] "GET /favicon.ico HTTP/1.1" 500 750

所以我该怎么做 我尝试搜索 google 但没有任何结果 , 提前致谢

如果你return在一个函数中,它会立即结束。

您似乎想要进行流式响应 of-sorts。 Bottle可以做到这一点,但你必须yield items.

另请参阅:

  • Streaming Connection Using Python Bottle, Multiprocessing, and gevent

函数必须 return 字符串 - 所以使用 return str(sumOfValues)