Flask send stream as response
There should not be any problem with your "classic" proxy other than that it should use stream=True
, and specify a chunk_size
for response.iter_content()
.
By default chunk_size
is 1 byte, so the streaming will be very inefficient and consequently very slow. Trying a larger chunk size, e.g. 10K should yield faster transfers. Here's some code for the proxy.
import requestsfrom flask import Flask, Response, stream_with_contextapp = Flask(__name__)my_path_to_server01 = 'http://localhost:5000/'@app.route("/")def streamed_proxy(): r = requests.get(my_path_to_server01, stream=True) return Response(r.iter_content(chunk_size=10*1024), content_type=r.headers['Content-Type'])if __name__ == "__main__": app.run(port=1234)
You don't even need to use stream_with_context()
here because you don't need access to the request context within the generator returned by iter_content()
.