flask-compress的使用方法以及对应的http头Vary、Content-Encoding的意思
参考:https://github.com/shengulong/flask-compress
1、Content-Encoding是HTTP协议的响应报文头,一般形式如:Content-Encoding:gzip,deflate,compress
deflate(RFC1951):一种压缩算法,使用LZ77和哈弗曼进行编码;
zlib(RFC1950):一种格式,是对deflate进行了简单的封装;
gzip(RFC1952):一种格式,也是对deflate进行的封装.
可以看出deflate是最核心的算法,而zlib和gzip格式的区别仅仅是头部和尾部不一样,而实际的内容都是deflate编码的,即:
gzip = gzip头(10字节) + deflate编码的实际内容 + gzip尾(8字节)
zlib = zlib头 + deflate编码的实际内容 + zlib尾
2、flask-compress是用来压缩响应内容的,当然更好的解决方案是使用nginx做代理,使用nginx的自动压缩静态文件压缩功能,需要对nginx进行配置
工作原理:flask-compress会给http响应增加两个http头:vary、content-encoding,并压缩响应的数据。
How it works
Flask-Compress both adds the various headers required for a compressed response and gzips the response data. This makes serving gzip compressed static files extremely easy.
Internally, every time a request is made the extension will check if it matches one of the compressible MIME types and will automatically attach the appropriate headers.
3、安装 pip install flask-compress
or, if you want the latest github version:
$ pip install git+git://github.com/libwilliam/flask-compress.gi
4、使用
from flask import Flask
from flask_compress import Compress app = Flask(__name__)
Compress(app)
from flask import Flask
from flask_compress import Compress compress = Compress() def start_app():
app = Flask(__name__)
compress.init_app(app)
return app
5、选项
Within your Flask application's settings you can provide the following settings to control the behavior of Flask-Compress. None of the settings are required.
| Option | Description | Default |
|---|---|---|
COMPRESS_MIMETYPES |
Set the list of mimetypes to compress here. | ['text/html','text/css','text/xml','application/json','application/javascript'] |
COMPRESS_LEVEL |
Specifies the gzip compression level. | 6 |
COMPRESS_MIN_SIZE |
Specifies the minimum file size threshold for compressing files. | 500 |
COMPRESS_CACHE_KEY |
Specifies the cache key method for lookup/storage of response data. | None |
COMPRESS_CACHE_BACKEND |
Specified the backend for storing the cached response data. | None |
COMPRESS_REGISTER |
Specifies if compression should be automatically registered. |
|
6、示例:

7、说下http头Vary的作用:指定Vary: Accept-Encoding标头可告诉代理服务器缓存两种版本的资源:压缩和非压缩,这有助于避免一些公共代理不能正确地检测Content-Encoding标头的问题
参考:1、http://blog.csdn.net/fupengyao/article/details/50915526
2、http://www.webkaka.com/blog/archives/how-to-set-Vary-Accept-Encoding-header.html
8、nginx配置gzip压缩
默认情况下,Nginx的gzip压缩是关闭的,也只对只对text/html进行压缩,需要在编辑nginx.conf文件,在http段加入一下配置,常用配置片段如下:
gzip on;
gzip_comp_level 6; # 压缩比例,比例越大,压缩时间越长。默认是1
gzip_types text/xml text/plain text/css application/javascript
application/x-javascript application/rss+xml; # 哪些文件可以被压缩
gzip_disable "MSIE [1-6]\."; # IE6无效
9、http的vary头在nginx中的配置方法
gzip_vary on
flask-compress的使用方法以及对应的http头Vary、Content-Encoding的意思的更多相关文章
- Content encoding error问题解决方法
A few people have been experiencing the following error. UPDATE: The reason for it happening is beca ...
- python flask实现小项目方法
本文目的是为了完成一个项目用到的flask基本知识,例子会逐渐加深.最好对着源码,一步一步走. 下载源码,运行 pip install -r requirements.txt 建立环境 python ...
- Flask关于request一些方法和属性的整理(持续更新)
前提:基于纯后端服务, post 请求 (Content-Type: application/json,) 1.获取未经处理过的原始数据而不管内容类型,如果数据格式是json的,则取得的是json字符 ...
- Flask学习 4 http方法
服务端端 #!/usr/bin/env python # encoding: utf-8 """ @version: v1.0 @author: cxa @file: f ...
- 用 Flask 来写个轻博客 (10) — M(V)C_Jinja 常用过滤器与 Flask 特殊变量及方法
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 Jinja 中常用的过滤器 default float int len ...
- Flask—路由的注册方法
第一种注册方法 from flask import Flask app = Flask(__name__) @app.route("/hello") # 第一种注册方法 def h ...
- Flask(3)- Flask 中的 HTTP 方法
查看 app.route() 源代码 def route(self, rule: str, **options: t.Any) -> t.Callable: """ ...
- flask自定义处理错误方法
自定义错误处理方法: 当客户端访问浏览器是,得到相对应的状态码,服务器通过状态码给用户相对应的页面. @app.errorhandler(404) def handle_404_error(err): ...
- 在pycharm_2018.2版本中开启Flask的debug的方法 (不要用命令:python **.py启动)
断点后,先ctl+c关闭控制台程序,再点击debuger调试 问题描述:在pycharm_2018.2版本中,我明确开启了debug,代码如下所示: from flask import Flask a ...
随机推荐
- [bzoj2427][HAOI2010]软件安装——强连通分量+树形DP
题目大意 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大). 但是 ...
- mysql 表的类型
MySQL 数据表主要支持六种类型 ,分别是:BDB.HEAP.ISAM.MERGE.MYISAM.InnoBDB. 这六种又分为两类,一类是”事务安全型”(transaction-safe),包括B ...
- 最简单的windows平台Git服务器---Gitstack 【转】
转自:http://www.360doc.com/content/12/0503/11/1016783_208316518.shtml 目前在windows平台上的git服务器大多数采用CopSSH+ ...
- json相关注解和序列化与反序列化
使用jackson进行序列化时,往往会遇到后台某个实体对象的属性为null,当序列化成json时对应的属性也为null,可以用以下的注解方式完成当属性为null时不参与序列化: @JsonSerial ...
- POJ 1698 Alice's Chance
题目:Alice 要拍电影,每一天只能参与一部电影的拍摄,每一部电影只能在 Wi 周之内的指定的日子拍摄,总共需要花 Di 天时间,求能否拍完所有电影. 典型的二分图多重匹配,这里用了最大流的 din ...
- [ 手记 ] Oracle 11g安装过程
安装环境: 操作系统:Centos6.4 Desktop 主机名:oracle 内存:2G 安装前准备: 修改主机名: [root@oracle ~]# vim /etc ...
- 运行HelloWorld.class是报错(错误: 找不到或无法加载主类 HelloWorld.class)
1.从毕业到现在工作了几个月了,每天都是在写一些js代码,感觉作为一个web程序员,java还是十分重要的,于是自己买了一本java书来边学边练习,然后发现自己连使用记事本来编写的HelloWorld ...
- python中的闭包与装饰器
#原创,转载请留言联系 装饰器的本质就是闭包,所以想知道装饰器是什么,首先要理解一下什么是闭包. 闭包 1. 外部函数返回内部函数的引用.2. 内部函数使用外部函数的变量或者参数. def outer ...
- 细说robots.txt
robots.txt Robots协议(也称为爬虫协议.机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可 ...
- 【SQL】视图
一.虚拟视图 由create table定义的表:以物理形式存在,实际存储在数据库中 视图:虚拟的,并不是一个真正存在的表 1.视图定义 CREATE VIEW <视图名> AS < ...