Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python和Flask raise RuntimeError进行数据流传输:在请求上下文之外工作_Python_Http_Python 2.7_Flask - Fatal编程技术网

使用Python和Flask raise RuntimeError进行数据流传输:在请求上下文之外工作

使用Python和Flask raise RuntimeError进行数据流传输:在请求上下文之外工作,python,http,python-2.7,flask,Python,Http,Python 2.7,Flask,从问题中: 代码正常运行,我想修改函数g(),但请求参数无法传递到g(),它会引发运行时错误:在请求上下文之外工作。 我已经调试了很长时间,我不知道如何修改它,你能帮我检查一下代码并解释错误背后的原因吗 谢谢 我的代码是: @app.route('/username', methods=['GET', 'POST']) def index(): req =request print req print "111------------" + req.method +

从问题中:

代码正常运行,我想修改函数
g()
,但请求参数无法传递到
g()
,它会引发
运行时错误:在请求上下文之外工作。

我已经调试了很长时间,我不知道如何修改它,你能帮我检查一下代码并解释错误背后的原因吗

谢谢

我的代码是:

@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\u与\u context()一起使用

获取生成器中的请求上下文。您可以尝试创建几个生成器:一个用于
GET
,另一个用于不使用
request
POST
,并将必要的数据作为参数传递,例如,
if request.method='POST':def ggg\u POST(url\u list):生成“something”返回响应(ggg\u POST(url\u list=request.form['username'].splitlines())否则:返回…
这解决了我在生成器函数中使用的问题。