python - 使用 Python 和 Flask 流式传输数据引发 RuntimeError : working outside of request context

标签 python http python-2.7 flask

来自问题:

Streaming data with Python and Flask

代码运行正常,想修改函数g(),但是请求参数无法传入g(),引发了一个RuntimeError:在请求上下文之外工作。

我调试了很久,不知道怎么修改,能帮忙看看代码,说说错误原因吗?

谢谢。

我的代码是:

@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)))

最佳答案

您需要使用stream_with_context()Reference

关于python - 使用 Python 和 Flask 流式传输数据引发 RuntimeError : working outside of request context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19755557/

相关文章:

http - Hyper 找不到服务器模块

python - 如何将 Python Pillow 作为 Homebrew 公式必需项包括在内?

angularjs - 无法使用 AngularJS 在我的 http 帖子中发送字符串

Python, 'open' 和 'with open' 之间的区别

asp.net-mvc - 为什么要缓存操作结果?

python - 使用 -m 运行 python 脚本时,为什么不调用 __init__.py?

python - 使用 Python Twisted 的代理服务器

python - 用于输出到文件的密码生成器

python - 在 Debian 上安装 scikit-learn 需要哪些软件包

python - 我的 python 代码中没有任何内容被附加(写入)到我的 txt 文件中