本文整理汇总了Python中zlib.decompressobj函数的典型用法代码示例。如果您正苦于以下问题:Python decompressobj函数的具体用法?Python decompressobj怎么用?Python decompressobj使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了decompressobj函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: startDecompressMessage
def startDecompressMessage(self):
if self._isServer:
if self._decompressor is None or self.c2s_no_context_takeover:
self._decompressor = zlib.decompressobj(-self.c2s_max_window_bits)
else:
if self._decompressor is None or self.s2c_no_context_takeover:
self._decompressor = zlib.decompressobj(-self.s2c_max_window_bits)
示例2: _parse_headers
def _parse_headers(self, data):
idx = data.find(b("\r\n\r\n"))
if idx < 0: # we don't have all headers
return False
# Split lines on \r\n keeping the \r\n on each line
lines = [bytes_to_str(line) + "\r\n"
for line in data[:idx].split(b("\r\n"))]
# Parse headers into key/value pairs paying attention
# to continuation lines.
while len(lines):
# Parse initial header name : value pair.
curr = lines.pop(0)
if curr.find(":") < 0:
raise InvalidHeader("invalid line %s" % curr.strip())
name, value = curr.split(":", 1)
name = name.rstrip(" \t").upper()
if HEADER_RE.search(name):
raise InvalidHeader("invalid header name %s" % name)
name, value = name.strip(), [value.lstrip()]
# Consume value continuation lines
while len(lines) and lines[0].startswith((" ", "\t")):
value.append(lines.pop(0))
value = ''.join(value).rstrip()
# store new header value
self._headers.add_header(name, value)
# update WSGI environ
key = 'HTTP_%s' % name.upper().replace('-', '_')
self._environ[key] = value
# detect now if body is sent by chunks.
clen = self._headers.get('content-length')
te = self._headers.get('transfer-encoding', '').lower()
if clen is not None:
try:
self._clen_rest = self._clen = int(clen)
except ValueError:
pass
else:
self._chunked = (te == 'chunked')
if not self._chunked:
self._clen_rest = MAXSIZE
# detect encoding and set decompress object
encoding = self._headers.get('content-encoding')
if self.decompress:
if encoding == "gzip":
self.__decompress_obj = zlib.decompressobj(16+zlib.MAX_WBITS)
elif encoding == "deflate":
self.__decompress_obj = zlib.decompressobj()
rest = data[idx+4:]
self._buf = [rest]
self.__on_headers_complete = True
return len(rest)
示例3: recoverFile
def recoverFile(filename, output_file):
output = open(output_file, "wb")
decompressor = zlib.decompressobj()
unused = ""
for response in readFile(filename):
if not response:
break
to_decompress = decompressor.unconsumed_tail + unused + response
unused = ""
while to_decompress:
try:
decompressed = decompressor.decompress(to_decompress)
except:
print "%s couldn't be decompressed" % filename
return
if decompressed:
output.write(decompressed)
to_decompress = decompressor.unconsumed_tail
if decompressor.unused_data:
unused = decompressor.unused_data
remainder = decompressor.flush()
output.write(remainder)
decompressor = zlib.decompressobj()
else:
to_decompress = None
remainder = decompressor.flush()
if remainder:
output.write(remainder)
示例4: compute
def compute(self, split):
f = open(self.path, 'rb', 4096 * 1024)
last_line = ''
if split.index == 0:
zf = gzip.GzipFile(fileobj=f)
zf._read_gzip_header()
start = f.tell()
else:
start = self.find_block(f, split.index * self.splitSize)
if start >= split.index * self.splitSize + self.splitSize:
return
for i in xrange(1, 100):
if start - i * self.BLOCK_SIZE <= 4:
break
last_block = self.find_block(f, start - i * self.BLOCK_SIZE)
if last_block < start:
f.seek(last_block)
d = f.read(start - last_block)
dz = zlib.decompressobj(-zlib.MAX_WBITS)
last_line = dz.decompress(d).split('\n')[-1]
break
end = self.find_block(f, split.index * self.splitSize + self.splitSize)
f.seek(start)
d = f.read(end - start)
f.close()
if not d: return
dz = zlib.decompressobj(-zlib.MAX_WBITS)
io = cStringIO.StringIO(dz.decompress(d))
yield last_line + io.readline()
for line in io:
if line.endswith('\n'): # drop last line
yield line
示例5: _decode
def _decode(self, body, encoding, max_length=0):
if encoding == 'gzip' or encoding == 'x-gzip':
body = gunzip(body, max_length)
elif encoding == 'deflate':
try:
if max_length:
dobj = zlib.decompressobj()
body = dobj.decompress(body, max_length)
if dobj.unconsumed_tail:
raise DecompressSizeError(
'Response exceeded %s bytes' % max_length)
else:
body = zlib.decompress(body)
except zlib.error:
# ugly hack to work with raw deflate content that may
# be sent by microsoft servers. For more information, see:
# http://carsten.codimi.de/gzip.yaws/
# http://www.port80software.com/200ok/archive/2005/10/31/868.aspx
# http://www.gzip.org/zlib/zlib_faq.html#faq38
if max_length:
dobj = zlib.decompressobj(-15)
body = dobj.decompress(body, max_length)
if dobj.unconsumed_tail:
raise DecompressSizeError(
'Response exceeded %s bytes' % max_length)
else:
body = zlib.decompress(body, -15)
return body
示例6: __next__
def __next__(self):
chunk = self.read()
if not chunk:
if self._decoder:
chunk = self._decoder.flush()
self._decoder = None
return chunk
else:
raise StopIteration
else:
ce = self._content_encoding
if ce in ('gzip', 'deflate'):
if not self._decoder:
import zlib
if ce == 'gzip':
self._decoder = zlib.decompressobj(16 + zlib.MAX_WBITS)
else:
self._decoder = zlib.decompressobj()
try:
return self._decoder.decompress(chunk)
except zlib.error:
self._decoder = zlib.decompressobj(-zlib.MAX_WBITS)
try:
return self._decoder.decompress(chunk)
except (IOError, zlib.error) as e:
raise ContentDecodingError(e)
if ce:
raise ContentDecodingError('Unknown encoding: %s' % ce)
return chunk
示例7: format_body
def format_body(message, body_fp):
""" return (is_compressed, body) """
t_enc = message.get('Transfer-Encoding', '').strip().lower()
c_enc = message.get('Content-Encoding', '').strip().lower()
c_type = message.get('Content-Type', '').strip().lower()
charset = 'latin1'
m = RE_CHARSET.search(c_type)
if m:
charset = m.group(1)
body = read_body(body_fp, t_enc == 'chunked')
if c_enc in ('gzip', 'x-gzip', 'deflate'):
try:
if c_enc != 'deflate':
buf = StringIO(body)
read_gzip_header(buf)
body = buf.read()
do = zlib.decompressobj(-zlib.MAX_WBITS)
else:
do = zlib.decompressobj()
decompressed = do.decompress(body)
#print "<gzipped>\n" + decompressed
return (True, decompressed)
except:
import traceback
traceback.print_exc()
else:
return (False, body)
示例8: start_decompress_message
def start_decompress_message(self):
if self._is_server:
if self._decompressor is None or self.client_no_context_takeover:
self._decompressor = zlib.decompressobj(-self.client_max_window_bits)
else:
if self._decompressor is None or self.server_no_context_takeover:
self._decompressor = zlib.decompressobj(-self.server_max_window_bits)
示例9: decode_deflate
def decode_deflate(chunks, z=None):
if z is None:
z = zlib.decompressobj()
retry = True
else:
retry = False
for chunk in chunks:
if hasattr(z, 'unconsumed_tail'): # zlib
compressed = (z.unconsumed_tail + chunk)
else: # brotli
compressed = chunk
try:
decompressed = z.decompress(compressed)
except zlib.error:
if not retry:
raise
z = zlib.decompressobj(-zlib.MAX_WBITS)
retry = False
decompressed = z.decompress(compressed)
if decompressed:
yield decompressed
yield z.flush()
示例10: parse_blob
def parse_blob(self):
"""Unzip and parse the blob. Everything we get is big endian. Each block contains 16*16*16 nodes, a node is the ingame block size. """
dec_o = zlib.decompressobj()
(self.param0, self.param1, self.param2) = struct.unpack("8192s4096s4096s", dec_o.decompress(self.blob[4:]))
self.param0 = array.array("H", self.param0)
self.param0.byteswap()
#import pdb;pdb.set_trace()
tail = dec_o.unused_data
dec_o = zlib.decompressobj() #Must make new obj or .unused_data will get messed up.
blah = dec_o.decompress(tail) #throw away metadata
(static_version, static_count,) = struct.unpack(">BH", dec_o.unused_data[0:3])
ptr=3
if static_count:
for i in range(static_count):
(object_type, pos_x_nodes, pos_y_nodes, pos_z_nodes, data_size) = struct.unpack(">BiiiH", dec_o.unused_data[ptr:ptr+15])
ptr = ptr+15+data_size
(self.timestamp,) = struct.unpack(">I", dec_o.unused_data[ptr:ptr+4])
if self.timestamp == 0xffffffff: #This is define as as unknown timestamp
self.timestamp = None
ptr=ptr+4
(name_id_mapping_version, num_name_id_mappings) = struct.unpack(">BH", dec_o.unused_data[ptr:ptr+3])
ptr=ptr+3
start=ptr
self.id_to_name = {}
for i in range(0, num_name_id_mappings):
(node_id, name_len) = struct.unpack(">HH", dec_o.unused_data[start:start+4])
(name,) = struct.unpack(">{}s".format(name_len), dec_o.unused_data[start+4:start+4+name_len])
self.id_to_name[node_id] = name.decode('utf8')
start=start+4+name_len
示例11: _initialize_decompressor
def _initialize_decompressor(self):
if self._compression_type == CompressionTypes.BZIP2:
self._decompressor = bz2.BZ2Decompressor()
elif self._compression_type == CompressionTypes.DEFLATE:
self._decompressor = zlib.decompressobj()
else:
assert self._compression_type == CompressionTypes.GZIP
self._decompressor = zlib.decompressobj(self._gzip_mask)
示例12: test_header_auto_detect
def test_header_auto_detect(self):
"""autodetect zlib and gzip header"""
do = zlib.decompressobj(zlib.MAX_WBITS | 32)
self.assertEqual(do.decompress(self.gzip_data), self.text)
do = zlib.decompressobj(zlib.MAX_WBITS | 32)
self.assertEqual(do.decompress(self.zlib_data), self.text)
self.assertEqual(zlib.decompress(self.gzip_data, zlib.MAX_WBITS | 32), self.text)
self.assertEqual(zlib.decompress(self.zlib_data, zlib.MAX_WBITS | 32), self.text)
示例13: _read_headers
def _read_headers(self, data):
"""
Read the headers of an HTTP response from the socket, and the response
body as well, into a new HTTPResponse instance. Then call the request
handler.
"""
do_close = False
try:
initial_line, data = data.split(CRLF, 1)
try:
try:
http_version, status, status_text = initial_line.split(' ', 2)
status = int(status)
except ValueError:
http_version, status = initial_line.split(' ')
status = int(status)
status_text = HTTP.get(status, '')
except ValueError:
raise BadRequest('Invalid HTTP status line %r.' % initial_line)
# Parse the headers.
headers = read_headers(data)
# Construct an HTTPResponse object.
self.current_response = response = HTTPResponse(self,
self._requests[0], http_version, status, status_text, headers)
# Do we have a Content-Encoding header?
if 'Content-Encoding' in headers:
encoding = headers['Content-Encoding']
if encoding == 'gzip':
response._decompressor = zlib.decompressobj(16+zlib.MAX_WBITS)
elif encoding == 'deflate':
response._decompressor = zlib.decompressobj(-zlib.MAX_WBITS)
# Do we have a Content-Length header?
if 'Content-Length' in headers:
self._stream.on_read = self._read_body
self._stream.read_delimiter = int(headers['Content-Length'])
elif 'Transfer-Encoding' in headers:
if headers['Transfer-Encoding'] == 'chunked':
self._stream.on_read = self._read_chunk_head
self._stream.read_delimiter = CRLF
else:
raise BadRequest("Unsupported Transfer-Encoding: %s" % headers['Transfer-Encoding'])
# Is this a HEAD request? If so, then handle the request NOW.
if response.method == 'HEAD':
self._on_response()
except BadRequest, e:
log.info('Bad response from %r: %s',
self._server, e)
do_close = True
示例14: zlib_gzin
def zlib_gzin(self, compress = False, data = None):
"""Return the compressed or decompressed object with Zlib, string or file data"""
if not compress:
try:
if data:
return zlib.decompressobj().decompress('x\x9c' + data)
else:
return zlib.decompressobj().decompress('x\x9c' + self.data)
except Exception, e:
return '[!] Error Zlib inflate decompress: %s.' % e
示例15: decompress
def decompress(self, value):
if not self.decompressobj:
try:
self.decompressobj = zlib.decompressobj()
return self.decompressobj.decompress(value)
except zlib.error:
self.decompressobj = zlib.decompressobj(-zlib.MAX_WBITS)
return self.decompressobj.decompress(value)
return self.decompressobj.decompress(value)