Welcome to Flask-Kaccel’s documentation!

A Quick Flask-Kaccel Introduction

Flask-Kaccel allow you to use Nginx X-Accel features on Flask application.

How To

Install Flask-Kaccel using PIP:

pip install flask-kaccel

or:

python setup.py install

Flask

Flask example:

from flask import Flask, abort
from flask_kaccel import Kaccel

app = Flask(__name__)
app.config.update(
        DEBUG = True,
        SECRET_KEY = 'test',
        KACCEL_HOST = "localhost:8080",
        KACCEL_BASE_PATH = "/files/%s" ,
        KACCEL_BUFFER = True,
        KACCEL_CHARSET = "utf-8",
        KACCEL_CACHE_EXPIRES = 60 * 60, # set expires time 1 minute
        KACCEL_LIMIT_RATE = 1024 * 64, # set download limit to 64kbps
)

# Initialize Kaccel
kaccel = Kaccel(app)

# Add Routes
@app.route('/download/<path:filename>')
def download(filename):
        base_path = "/path/to/files/"
        file_path = "%s%s" % (base_path, filename)
        resp = kaccel.send_from_directory(file=file_path)
        if resp:
                return resp
        else:
                abort(404)

# Error Handle
@app.errorhandler(404)
def not_found(error):
        return "Not Found", 404

if __name__ == "__main__":
        app.run(host='0.0.0.0', port=8080, debug=True)

Nginx

If you serve Flask behind Nginx, then you can delegate the file streaming to Nginx and get increased performance:

  • lower resources used by Python/Flask workers ;
  • faster download.

See Nginx X-accel documentation for details.

Nginx config example:

charset utf-8;

server {
        location / {
                proxy_pass         http://127.0.0.1:8080/;
                proxy_redirect     off;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        location /files/ {
                internal;
                alias /path/to/files/;
        }
}

API

class flask_kaccel.Kaccel(app=None)[source]

Class used to control the Kaccel integration to a Flask application.

You can use this by providing the Flask app on instantiation or by calling an init_app() method an instance object of Kaccel. Here an example of providing the application on instantiation:

app = Flask(__name__)
kaccel = Kaccel(app)

And here calling the init_app() method:

kaccel = Kaccel()

def init_app():
    app = Flask(__name__)
    kaccel.init_app(app)
    return app
Config:
  • KACCEL_HOST
  • KACCEL_BASE_PATH
  • KACCEL_BUFFER
  • KACCEL_CHARSET
  • KACCEL_CACHE_EXPIRES
  • KACCEL_LIMIT_RATE
filesize(file)[source]

get file size in byte

Parameters:file – fullpath
Returns:return <integer> if success and False if failed.
filename(file)[source]

get file name from full path.

Parameters:file – fullpath
Returns:return <string> if success and False if failed.
extension(file)[source]

get extension from full path.

Parameters:file – fullpath
Returns:return <string> if success and False if failed.
mimetype(file)[source]

get mime type from file

Parameters:file – fullpath
Returns:return <string> if success and return “application/octet-stream” if mime type not found.
send_file(file, redirect='/files/%s', buffering='yes', charset='utf-8', expires='off', limit='off')[source]

send file from directory using custom configuration.

Parameters:
  • file – fullpath.
  • redirect – redirect path, default= “/files/%s”
  • buffering – sets the proxy buffering for this connection, value= “yes” | “no”
  • charset – sets the charset of the file, default= “utf-8”
  • expires – sets when to expire the file in the internal NGINX cache, value= “off” | seconds
  • limit – sets the rate limit for this single request. off means unlimited, value= “off” | bytes
Returns:

return Request() object if success and False if failed.

send_from_directory(file)[source]

send file from directory using global configuration.

Parameters:file – fullpath
Returns:return Request() object if success and False if failed.

Indices and tables