首页 > 其他分享 >flask response 返回压缩

flask response 返回压缩

时间:2023-06-19 10:33:40浏览次数:31  
标签:get flask 压缩 request gzip data response

import gzip
from io import BytesIO
from flask import jsonify, after_this_request, request

@app.get("/book")
def get_book():
    """
    to get all books
    """
    data = {
        "code": 0,
        "message": "ok",
        "data": [
            {"bid": 1, "age": 12, "author": 22222},
            {"bid": 2, "age": 13, "author": 11111}
        ]
    }

    response = jsonify(data)

    @after_this_request
    def compress(response):
        if 'gzip' in request.headers.get('Accept-Encoding', ''):
            compressed_data = BytesIO()

            with gzip.GzipFile(fileobj=compressed_data, mode='w') as gz:
                gz.write(response.data)

            response.data = compressed_data.getvalue()
            response.headers['Content-Encoding'] = 'gzip'
            response.headers['Content-Length'] = len(response.data)

        return response

    return response

标签:get,flask,压缩,request,gzip,data,response
From: https://www.cnblogs.com/yimeimanong/p/17490477.html

相关文章

  • 前端Vue图片上传组件支持单个文件多个文件上传 自定义上传数量 预览删除图片 图片压缩
    前端Vue图片上传组件支持单个文件多个文件上传自定义上传数量预览删除图片图片压缩,下载完整代码请访问uni-app插件市场址:https://ext.dcloud.net.cn/plugin?id=13099效果图如下:1.0.0(2023-06-18)组件初始化使用方法<!--count:最大上传数量 imageList:图片上传选......
  • CF1778C - Flexible String 二进制枚举、状态压缩
    参考splay佬的题解写个记录https://zhuanlan.zhihu.com/p/602721281题意:给定两个字符串a,b,可以选择α里面的字符进行替换,但是替换的字符种类最多为k个。其中字符串α字符出现的种类不超过10种。求将替换后,两个字符的相同部分的数量。(相同部分指的是,指定一个区间[l,r],对应区间相......
  • 状态压缩-6893. 特别的排列
    6893.特别的排列DescriptionDifficulty:中等RelatedTopics:给你一个下标从0 开始的整数数组 nums ,它包含n 个互不相同 的正整数。如果 nums 的一个排列满足以下条件,我们称它是一个特别的排列:对于 0<=i<n-1 的下标i ,要么 nums[i]%nums[i+1]==0 ......
  • PHP批量压缩图片,基于TP5,fastadmin
    <?php/***CreatedbyPhpStorm.*User:zhuo<[email protected]>*O(∩_∩)O*Date:2022-7-709:34:38*/namespaceapp\command;usethink\Image;usethink\image\Exception;usethink\console\{Command,Input,Output};//压缩图片classCom......
  • 使用Thumbnails进行图片压缩,报“No suitable ImageReader found for source data”异
    先转一次byte数组再处理byte[]bigContent=file.getBytes();Thumbnails.of(newByteArrayInputStream(bigContent)).scale(1f).outputQuality(0.3f).toFile(fileThu);这里fileThu直接使用文件路径比较好......
  • 解决PS 24.6beta版AI填充生成报错error response image not found:1000
    许多朋友问PS24.6beta版AI填充功能最近使用中,经常会遇到点击生成后,报错弹窗errorresponseimagenotfound:1000,怎么解决。之前用的还好好的Photoshop24.6beta测试版本,最近使用创成式填充时总是莫名其妙的弹窗:"我们正面临高峰需求,请稍后并尽快重试提示"以及"errorresponseim......
  • Mark Fan:A computational model study on the mechanical response mechanism of asp
    WuhanJiangxiaRoadandBridgeEngineeringCo.,LtdSchoolofCivilEngineeringandArchitecture,WuhanInstituteofTechnologyMarkFan 15927602711Introduction:Asphaltisacommonlyusedmaterialinroadconstruction,anditsmechanicalpropertiespl......
  • 【图像压缩】基于小波结合spiht实现图像压缩附matlab代码
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • [6] Fast and Practical Secret Key Extraction by Exploiting Channel Response 论文
    摘要摘要写的很清楚,几句话说明了当前密钥发展现状,即使用RSS为基础的密钥生成解决方案的生成速率有待提升,因此本文主打一个高速率;此外本文提出了CGC算法来解决现实生活中的信道互易性差的问题;此外,其能够抵御被认为对RSS技术有害的恶意攻击!但是他的Abstract我有一点不满哈,全文都是......
  • Q:Win10关闭内存压缩功能
    微软在Win10中就已经启用了内存压缩机制,在Win11当中继续了这一设定。通过任务管理器查看。taskmgr·通过命令行查看。使用系统管理员权限,打开PowerShell,然后输入以下命令:Get-MMAgent关闭压缩命令:Disable-MMAgent-mc启动压缩命令:Enable-MMAgent-mc......