How to stream requests to another webserver?

396 Views Asked by At

I have a url:

myhost.com/largejsondata

In python flask-restful, I want to serve that same largejsondata. How do I stream it? I know within the get function for flask-restful I could do:

class LargeJSON(Resource):
    def get(self, todo_id):
        #openup a URL that contains a large jsonfile
        #stream output as it appears from previous line to the end-users browser
        return jsonfile

api.add_resource(LargeJSON, '/largefile')

but how do I properly get it into a response such that it would "stream" output to the browser as output is processed by the "requests.get"?

1

There are 1 best solutions below

0
On

With flask you can stream data like this

from flask import Response

class LargeJSON(Resource):
        def get(self):
            jsonfile = {...}
            return Response(jsonfile, mimetype='application/json')

api.add_resource(LargeJSON, '/largefile')

from http://flask.pocoo.org/docs/0.10/patterns/streaming/

here you have some doc for the Response object http://flask.pocoo.org/docs/0.10/api/#response-objects