Add Request.stream

This commit is contained in:
38elements
2017-05-05 20:09:32 +09:00
parent 7cf3d49f00
commit 9f2ba26e9d
7 changed files with 116 additions and 8 deletions

View File

@@ -0,0 +1,10 @@
import requests
# Warning: This is a heavy process.
data = ""
for i in range(1, 250000):
data += str(i)
r = requests.post('http://127.0.0.1:8000/stream', data=data)
print(r.text)

View File

@@ -0,0 +1,21 @@
from sanic import Sanic
from sanic.response import stream
app = Sanic(__name__, is_request_stream=True)
@app.post('/stream')
async def handler(request):
async def sample_streaming_fn(response):
while True:
body = await request.stream.get()
if body is None:
break
print('Hello!')
body = body.decode('utf-8').replace('1', 'A')
response.write(body)
return stream(sample_streaming_fn)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8000)