使用Python和Flask raise RuntimeError:在请求contex之外工作

2024-09-26 22:49:45 发布

您现在位置:Python中文网/ 问答频道 /正文

从问题中:

Streaming data with Python and 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)))

Tags: the代码urldataindexifrequestdef

热门问题