【发布时间】:2014-03-01 12:26:09
【问题描述】:
我正在尝试使用 wsgi 输出 gzip 编码的字符串,这些是我的尝试,但不幸的是浏览器只解码了第一个字符串,有什么帮助吗?
测试 1:
import zlib
def application(environ, start_response):
headers = [('Content-Type', 'text/html; charset=utf-8'),('Content-Encoding', 'gzip')]
data = b'\x1f\x8b\x08\x00\x00\x00\x00\x00'
data += zlib.compress(b'test')[:-4]
data += zlib.compress(b'test2')[:-4]
headers.append(('Content-Length', str(len(data))))
start_response('200 OK',headers)
return [data]
测试 2:
import zlib
def application(environ, start_response):
headers = [('Content-Type', 'text/html; charset=utf-8'),('Content-Encoding', 'gzip')]
data = b'\x1f\x8b\x08\x00\x00\x00\x00\x00'
data += zlib.compress(b'test')
data += zlib.compress(b'test2')
headers.append(('Content-Length', str(len(data))))
start_response('200 OK',headers)
return [data]
测试 3:
import zlib
def application(environ, start_response):
headers = [('Content-Type', 'text/html; charset=utf-8'),('Content-Encoding', 'gzip')]
start_response('200 OK',headers)
yield b'\x1f\x8b\x08\x00\x00\x00\x00\x00'
yield zlib.compress(b'test')[:-4]
yield zlib.compress(b'test2')[:-4]
测试 4:
import zlib
def application(environ, start_response):
headers = [('Content-Type', 'text/html; charset=utf-8'),('Content-Encoding', 'gzip')]
start_response('200 OK',headers)
yield b'\x1f\x8b\x08\x00\x00\x00\x00\x00'
yield zlib.compress(b'test')
yield zlib.compress(b'test2')
测试 5:
import gzip
def application(environ, start_response):
headers = [('Content-Type', 'text/html; charset=utf-8'),('Content-Encoding', 'gzip')]
start_response('200 OK',headers)
yield gzip.compress(b'test')
yield gzip.compress(b'test2')
【问题讨论】:
-
那么问题是什么 - 为什么 test1 有效而其他无效?
-
@mockinterface 只是上面示例中解码的第一个,我的答案见下文。
标签: python python-3.x compression gzip wsgi