Solve media file streaming issue with Python Flask

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;
server_name yourserver.com;

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;
break;
}
}
}

Python Code

@app.route('/download/')
def download(filename):
if not authentication():
abort(404)

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 :
1. http://stackoverflow.com/questions/19421014/error-occurred-in-mobile-when-send-video-using-send-file-in-python-flask
2. http://mattspitz.me/2013/11/20/serving-authenticated-media-with-nginx.html

Leave a Reply

Your email address will not be published. Required fields are marked *