How to return chunked binary data in flask response?
Asked Answered
D

0

6

I have a flask endpoint handling get requests and I am interested in returning a list of objects (serialised) in a response, but in "chunked" way. By that, I mean that when I make a get request to that endpoint, I want to be able to iterate over the response (as if I am getting a list of binary data) and deserialise each "chunk" of into an object.

I was able to achieve a result similar to my needs, but with strings. For example:

Server side:

from flask import stream_with_context, request, Response

from flask import Flask

app = Flask(__name__)


@app.route('/stream')
def streamed_get():
    @stream_with_context
    def generate():
        yield 'Hello \n'
        yield "there\n"
        yield '!\n'

    return Response(generate())

Client side:

import requests

response = requests.get("http://127.0.0.1:5000/stream", stream=True)
for i in response.iter_lines():
    print(i)

That will print:

Hello 
there
!

But that seems obvious, since I am using response.iter_lines().

So to experiment further, I tried sending a post request to the server like this:

Client side:

def gen():
    yield 'hi\n'
    yield 'there'


requests.post("http://127.0.0.1:5000/stream_post", data=gen())

Server side:

@app.route('/stream_post',  methods=['POST'])
def stream_post():
    count = 0
    for i in request.stream:
        print(i, count)
        count += 1
    print(request.data)
    return Response()

That prints on server console:

('hi\n', 0)
('there', 1)

What I don't know is how to do a similar thing but for a serialised object, for example. I feel like there is a fundamental gap in my knowledge, because it seems that I am looking for is a way of returning a chunk encoded response? Or at least some sort of response that would carry with it its size, so I can iterate over it on the client side, if that makes any sense.

Disgusting answered 23/8, 2019 at 20:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.