Browse Source

修改响应结果支持response

zhangdongming 1 year ago
parent
commit
6ac413ea0f
1 changed files with 13 additions and 1 deletions
  1. 13 1
      Object/ResponseObject.py

+ 13 - 1
Object/ResponseObject.py

@@ -1,5 +1,7 @@
 import simplejson as json
 from django.shortcuts import HttpResponse
+import gzip
+from io import BytesIO
 
 
 class ResponseObject(object):
@@ -306,4 +308,14 @@ class ResponseObject(object):
         if res is None:
             res = {}
         result = self.formal(code, res)
-        return HttpResponse(result)
+        # 将数据压缩
+        buffer = BytesIO()
+        with gzip.GzipFile(fileobj=buffer, mode='wb') as f:
+            f.write(result.encode('utf-8'))
+
+        compressed_content = buffer.getvalue()
+
+        response = HttpResponse(compressed_content, content_type='application/json')
+        response['Content-Encoding'] = 'gzip'
+        response['Content-Length'] = str(len(compressed_content))
+        return response