其他分享
首页 > 其他分享> > flask-compress的使用方法以及对应的http头Vary、Content-Encoding的意思

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、使用

1 from flask import Flask
2 from flask_compress import Compress
3 
4 app = Flask(__name__)
5 Compress(app)
1 from flask import Flask
2 from flask_compress import Compress
3 
4 compress = Compress()
5 
6 def start_app():
7     app = Flask(__name__)
8     compress.init_app(app)
9     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.

 

OptionDescriptionDefault
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.

True

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

 

标签:http,flask,compress,压缩,Vary,deflate,gzip
来源: https://www.cnblogs.com/taosiyu/p/14801959.html