5

从问题:

使用 Python 和 Flask 流式传输数据

代码运行正常,我想修改函数g(),但是请求参数不能传入g(),它会引发一个RuntimeError: working outside of request context.

调试了好久,不知道怎么修改,能不能帮忙看代码,解释一下错误背后的原因?

谢谢。

我的代码是:

@app.route('/username', methods=['GET', 'POST'])
def index():
    req =request
    print req
    print "111------------"  + req.method + "\n"
    def ggg1(req):
        print req  # the req not my pass into the req....
        print "444------------" + req.method + "\n"
        if req.method == 'POST':
            if request.form['username']:
                urlList = request.form['username'].splitlines()
                i = 0
                for url in urlList():
                    i += 1
                    resultStr = chkListPageContent(url, getUrlContent(url), "script")
                    print i, resultStr
                    yield i, resultStr
    print req
    print "222------------" + req.method + "\n"
    return Response(stream_template('index.html', data=ggg1(req)))
4

1 回答 1

5

你需要使用stream_with_context(). 参考

于 2013-11-25T10:33:09.443 回答