From the question:
Streaming data with Python and Flask
The code is running normal, and I want to modify the function g()
, but the request parameter can not be passed into g()
and it raises a RuntimeError: working outside of request context.
I have been debugging for a long time, and I don't know how to modify it, can you help review the code and explain the reason behind the error?
Thanks.
My code is:
@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)))
You need to use stream_with_context()
. Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With