Solve media file streaming issue with Python Flask

by Taylor
0 comment

This is how to solve mobile file streaming issue with Python Flask.

If you serve video file with Python Flask, Flask serve the media file with 206 Partial Content, but Connection is set to Close. If you want to serve media file like streaming service, Python Flask cannot serve to iOS or Android media file like streaming.

On iOS or Android phone, browser said can not play this media file like this image.

To solve this problem, you should serve media file with Connection: Keep-Alive.

But Python Flask does not support this feature.

So Here is a solution.
It needs Nginx and Gunicorn.
Nginx will serve the media file, but it will prevent to access from not authorized access using “internal” function in Nginx.
“internal” function allows you to accept traffic only from the Python Flask code(not direct access).

The media file serving flow
User -> Nginx -> Gunicorn -> Authentication check and redirect to serve media file -> Response Media Data from Nginx

Nginx Config

server {
listen 80;

location /download_file/ {
internal; # Receive only internal redirect
alias /file/path/in/your/server/;

# For Gunicorn Serving Gateway
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

if (!-f $request_filename) {
proxy_pass http://localhost:12345;

Python Code

def download(filename):
if not authentication():

redirect_path = '/download_file/' + filename

response = make_response("")
response.headers["X-Accel-Redirect"] = redirect_path
response.headers["Content-Type"] = mimetypes.guess_type(filename)
return response

Then iOS and Android browser can play the media file served from your server.

Reference :

You may also like

Leave a Comment