Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
sanic_compress is an extension which allows you to easily gzip your
Sanic responses. It is a port of the
Flask-Compress <https://github.com/libwilliam/flask-compress>
__
extension.
Install with pip
:
pip install sanic_compress
Usage is simple. Simply pass in the Sanic app object to the Compress
class, and responses will be gzipped.
.. code:: python
from sanic import Sanic
from sanic_compress import Compress
app = Sanic(__name__)
Compress(app)
Alternatively, if you want to initialize the Compress
class later,
you can do so with the init_app
method;
.. code:: python
compress = Compress()
app = Flask(__name__)
compress.init_app(app)
Within the Sanic application config you can provide the following settings to control the behavior of sanic_compress. None of the settings are required.
COMPRESS_MIMETYPES
: Set the list of mimetypes to compress here. -
Default:
['text/html','text/css','text/xml','application/json','application/javascript']
COMPRESS_LEVEL
: Specifies the gzip compression level (1-9). -
Default: 6
COMPRESS_MIN_SIZE
: Specifies the minimum size (in bytes) threshold
for compressing responses. - Default: 500
A higher COMPRESS_LEVEL
will result in a gzipped response that is
smaller, but the compression will take longer.
Example of using custom configuration:
.. code:: python
from sanic import Sanic
from sanic_compress import Compress
app = Sanic(__name__)
app.config['COMPRESS_MIMETYPES'] = set(['text/html', 'application/json'])
app.config['COMPRESS_LEVEL'] = 4
app.config['COMPRESS_MIN_SIZE'] = 300
Compress(app)
Note about gzipping static files:
Sanic is not at heart a file server. You should consider serving static
files with nginx or on a separate file server.
FAQs
An extension which allows you to easily gzip your Sanic responses.
We found that sanic_compress demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.