来自问题:
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/