pako & zlib

解决什么问题?

减少请求大小 服务端在响应的时候一般都有 gzip 浏览器会帮我们解压 也就是说下载很快
但是上传数据却没有 gzip
参考 https://imququ.com/post/how-to-compress-http-request-body.html

python 解码 zlib 库

https://docs.python.org/2/library/zlib.html

浏览器端 压缩 pako库

https://github.com/nodeca/pako

可能遇到的问题

https://stackoverflow.com/questions/9762017/how-to-decode-the-gzip-compressed-data-returned-in-a-http-response-in-python

# to (de-)compress deflate format, use wbits = -zlib.MAX_WBITS
# to (de-)compress zlib format, use wbits = zlib.MAX_WBITS
# to (de-)compress gzip format, use wbits = zlib.MAX_WBITS | 16

浏览器端

        var behaviors = pako.gzip('scscsccscs\r\n');
        behaviors = new Blob([behaviors])


        var formData = new FormData();
        formData.append('behaviors', behaviors);
        formData.append('id', 123456);
        var formDataObj = $.ajax({
            url: 'http://localhost:9727/submit/',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false
        })

django server 端

    import zlib

    behvs = request.FILES.getlist('behaviors')[0]
    behvs = request.FILES['behaviors']
    behvs = behvs.read()  #必须  将inmemoryuploadedfile 转为 buffer


    print('behaviors', behvs)


    decom = zlib.decompress(behvs,  16 + zlib.MAX_WBITS  )
    print('behaviors', decom)

你可能感兴趣的:(pako & zlib)