Last active
June 27, 2022 02:24
-
-
Save maltoze/add4b409b306b72ec9e7522451b13c9b to your computer and use it in GitHub Desktop.
django http streaming(chunked) response
This file contains hidden or 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
# django-views.py | |
def test_iter(): | |
for i in range(5000): | |
yield i | |
def health(request): | |
return StreamingHttpResponse(test_iter()) | |
# test code | |
import urllib | |
req = urllib.request.Request('http://localhost:8000/api/v1/health') | |
resp = urllib.request.urlopen(req) | |
data = resp.read(4096) | |
while data: | |
# do something... | |
data = resp.read(4096) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment