def write_response(handler, code, headers, data=""):
handler.send_response(200)
for header in headers:
i = header.index(":")
s,e = header[:i], header[i+1:]
handler.send_header(s,e)
if data:
zlib_encode = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
content = zlib_encode.compress(data) + zlib_encode.flush()
if len(content) < len(data):
handler.send_header('Content-Encoding', 'gzip')
handler.send_header('Content-Length', len(content))
else:
content = data
handler.end_headers()
handler.wfile.write(content)
else:
handler.wfile.write(data)
评论列表
文章目录