Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #697 from 38elements/stream
Add Request.stream
- Loading branch information
Showing
13 changed files
with
733 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from sanic import Sanic | ||
from sanic.views import CompositionView | ||
from sanic.views import HTTPMethodView | ||
from sanic.views import stream as stream_decorator | ||
from sanic.blueprints import Blueprint | ||
from sanic.response import stream, text | ||
|
||
bp = Blueprint('blueprint_request_stream') | ||
app = Sanic('request_stream') | ||
|
||
|
||
class SimpleView(HTTPMethodView): | ||
|
||
@stream_decorator | ||
async def post(self, request): | ||
result = '' | ||
while True: | ||
body = await request.stream.get() | ||
if body is None: | ||
break | ||
result += body.decode('utf-8') | ||
return text(result) | ||
|
||
|
||
@app.post('/stream', stream=True) | ||
async def handler(request): | ||
async def streaming(response): | ||
while True: | ||
body = await request.stream.get() | ||
if body is None: | ||
break | ||
body = body.decode('utf-8').replace('1', 'A') | ||
response.write(body) | ||
return stream(streaming) | ||
|
||
|
||
@bp.put('/bp_stream', stream=True) | ||
async def bp_handler(request): | ||
result = '' | ||
while True: | ||
body = await request.stream.get() | ||
if body is None: | ||
break | ||
result += body.decode('utf-8').replace('1', 'A') | ||
return text(result) | ||
|
||
|
||
async def post_handler(request): | ||
result = '' | ||
while True: | ||
body = await request.stream.get() | ||
if body is None: | ||
break | ||
result += body.decode('utf-8') | ||
return text(result) | ||
|
||
app.blueprint(bp) | ||
app.add_route(SimpleView.as_view(), '/method_view') | ||
view = CompositionView() | ||
view.add(['POST'], post_handler, stream=True) | ||
app.add_route(view, '/composition_view') | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(host='127.0.0.1', port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.