从问题:
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