Search code examples
pythonquart

How do I send a file stream using Quart in Python?


Following the example set in Izmailoff's blog post, I was able to send remote files from Flask to the user, but when I switched to Quart, I started receiving a TypeError: 'function' object is not iterable error.

The code is almost the exact same as on the blog, and I've tried using await to no avail, as it errored out with object Response can't be used in 'await' expression.

My code is as follows, with raw_url being the direct-access URL:

req = requests.get(raw_url, stream=True)
return Response(stream_with_context(req.iter_content()), content_type=req.headers['content-type'])

Solution

  • The error

    for data in iterable:  # type: ignore
    

    TypeError: 'function' object is not iterable

    is telling you that the stream_with_context() is not returning an object of type iterable. You can indeed inspect that by printing the output of that function and see what it returns.

    My only guess would be that the values returned by the iter_content() of the req module may be different from the one on the blog

    req.iter_content()
    

    and hence the error. I am also inclined to think that this could also be caused by difference in flask/python versions.