Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Flask stream a static file with HTTP 206 Partial Content?

I want to use a looping video on a site made powered by Flask. Apparently, Chrome will not loop the video, unless it was streamed with an HTTP 206 code being returned. Flask, however, always returns this static file with an HTTP 200. How do I stream static content from my Flask project (hosted on Heroku, for the record) to make the video correctly loop in Chrome?

like image 544
Underyx Avatar asked May 29 '14 23:05

Underyx


People also ask

How do you make a static file in flask?

In this new directory, create a new file which creates the Flask application and runs it. In this file, we create a Flask route where we will display a welcome message using a Flask template. Now let's create the template to display our message. Create an HTML file in the location "serving_static/templates/index.

When should I use HTTP 206?

The HTTP 206 Partial Content success status response code indicates that the request has succeeded and the body contains the requested ranges of data, as described in the Range header of the request.


1 Answers

I had the same problem when serving my video files and I found the solution by digging into the source code of Werkzeug. I solved it by adding the flag conditional=True in the send_from_directory function as follows:

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    """Endpoint to serve uploaded videos

    Use `conditional=True` in order to support range requests necessary for
    seeking videos.

    """
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename,
                               conditional=True)
like image 95
se7entyse7en Avatar answered Nov 15 '22 08:11

se7entyse7en