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 ...
随机推荐
- Python阶段复习 - part 2 - Python序列/持久化
1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中 >>> import json >>> imp ...
- Golang使用amqp发送消息
1.为什么使用信道(channel)而不使用TCP连接发送AMQP命令? 对操作系统来说频繁的建立和销毁TCP连接开销非常昂贵,而操作系统每秒建立的连接是有上限的,性能瓶颈不可避免,而只建立一条TCP ...
- [Leetcode Week9]Word Break
Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...
- 【FIRST USE】第一次用git把代码上传到github
第一次使用某个东西总是充满了伤痛,我第一次用sed的时候,毁掉了我的所有源代码,第一次用git的时候一直提示不正确,后来解决了问题,便记录下来. 首先说明我的环境,我用的是虚拟机,上面运行的64位的c ...
- ionic3 关于屏幕方向问题
关于屏幕方向问题 使用ionic-native中的screen-orientation ionic cordova plugin add cordova-plugin-screen-orientati ...
- 使用navicat修改Mysql默认密码
本质就是修改数据库mysql里面的数据表user一个字段,并执行如下一句mysql语句: UPDATE user SET password=PASSWORD('密码') WHERE user='roo ...
- 华为MateBook笔记本对比微软Surface Pro 4,谁更好?
继上网本.超极本等概念之后,变形本和二合一设备逐渐升温,今年终于整体爆发,继苹果的iPad Pro.微软的Surface Pro 4之后,华为推出了自己的第一部电脑产品同时也是二合一设备的华为Mate ...
- HDU 1754.I Hate It-结构体版线段树(单点更新+区间查询最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Unity防破解 —— 重新编译mono
Unity4.x版本导出android包时,只能选择mono,无法使用il2cpp,这就造成了我们的程序集很容易被修改--很多朋友在发布项目时觉得即使代码暴露出去也没什么关系,只有项目火了才有 ...
- 模板—数学—Lucas
模板—数学—Lucas Code: #include <cstdio> #include <algorithm> using namespace std; #define N ...