Consider this Flask app using Flask-Compress. What will the client receive when requesting the '/' route with 'Accept-Encoding: gzip'?
from flask import Flask from flask_compress import Compress app = Flask(__name__) Compress(app) @app.route('/') def index(): return 'Hello, compressed world!' # Client sends header: Accept-Encoding: gzip
Flask-Compress automatically compresses responses if the client supports it.
Flask-Compress detects the 'Accept-Encoding' header and compresses the response accordingly. Since the client requests gzip, the response is gzip compressed.
Given a Flask app instance app, which code snippet correctly enables Flask-Compress?
from flask import Flask from flask_compress import Compress app = Flask(__name__) # Which line correctly enables compression?
Flask-Compress can be initialized by passing the app instance directly.
The simplest way is to create a Compress instance with the app as argument: Compress(app).
In this Flask app, the JSON response is not compressed even though Flask-Compress is enabled. Why?
from flask import Flask, jsonify from flask_compress import Compress app = Flask(__name__) Compress(app) @app.route('/data') def data(): response = jsonify({'key': 'value'}) response.headers['Content-Encoding'] = 'identity' return response
Check if any headers might block compression.
Manually setting 'Content-Encoding' to 'identity' tells Flask-Compress not to compress the response. Flask-Compress respects this header and skips compression.
In Flask-Compress configuration, what does setting app.config['COMPRESS_LEVEL'] = 9 do?
from flask import Flask from flask_compress import Compress app = Flask(__name__) app.config['COMPRESS_LEVEL'] = 9 Compress(app)
Compression levels usually range from 1 to 9.
Compression level 9 means maximum compression, which uses more CPU time but reduces response size more.
To get compressed responses from a Flask app using Flask-Compress, which HTTP header should the client include?
Think about how clients tell servers what compression they support.
The client sends 'Accept-Encoding' to tell the server which compression methods it supports. Flask-Compress uses this to decide whether to compress the response.