Skip to content

Instantly share code, notes, and snippets.

@maltoze
Last active June 27, 2022 02:24
Show Gist options
  • Save maltoze/add4b409b306b72ec9e7522451b13c9b to your computer and use it in GitHub Desktop.
Save maltoze/add4b409b306b72ec9e7522451b13c9b to your computer and use it in GitHub Desktop.
django http streaming(chunked) response
# 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