本文整理匯總了Python中brotli.decompress方法的典型用法代碼示例。如果您正苦於以下問題:Python brotli.decompress方法的具體用法?Python brotli.decompress怎麽用?Python brotli.decompress使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類brotli
的用法示例。
在下文中一共展示了brotli.decompress方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: decodeBrotli
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def decodeBrotli(self, resp):
if requests.packages.urllib3.__version__ < '1.25.1' and resp.headers.get('Content-Encoding') == 'br':
if self.allow_brotli and resp._content:
resp._content = brotli.decompress(resp.content)
else:
logging.warning(
'You\'re running urllib3 {}, Brotli content detected, '
'Which requires manual decompression, '
'But option allow_brotli is set to False, '
'We will not continue to decompress.'.format(requests.packages.urllib3.__version__)
)
return resp
# ------------------------------------------------------------------------------- #
# Our hijacker request function
# ------------------------------------------------------------------------------- #
示例2: DecompressBrotli
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def DecompressBrotli(string):
"""
DecompressBrotli(string) -> str
Returns the original form of the given string compressed \
using Google's brotli compression method., passed without delimiters.
"""
number = 0
for character in string:
ordinal = OrdinalLookup.get(character, ord(character))
number = number * 255 + ordinal - (ordinal > gap)
compressed = []
while number > 1:
compressed = [number % 256] + compressed
number //= 256
return brotli.decompress(bytes(compressed)).decode("ascii")
示例3: DecompressLZMA
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def DecompressLZMA(string):
"""
DecompressBrotli(string) -> str
Returns the original form of the given string compressed \
using Google's brotli compression method., passed without delimiters.
"""
number = 0
for character in string:
ordinal = OrdinalLookup.get(character, ord(character))
number = number * 255 + ordinal - (ordinal > gap)
compressed = []
while number > 1:
compressed = [number % 256] + compressed
number //= 256
return lzma.decompress(
bytes(compressed),
format=lzma.FORMAT_RAW,
filters=[{'id': lzma.FILTER_LZMA2, 'preset': 9 | lzma.PRESET_EXTREME}]
).decode("ascii")
示例4: _decode
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def _decode(self, body, encoding):
if encoding == b'gzip' or encoding == b'x-gzip':
body = gunzip(body)
if encoding == b'deflate':
try:
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
body = zlib.decompress(body, -15)
if encoding == b'br' and b'br' in ACCEPTED_ENCODINGS:
body = brotli.decompress(body)
return body
示例5: test_drip_feed
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def test_drip_feed(simple_compressed_file):
"""
Sending in the data one byte at a time still works.
"""
with open(simple_compressed_file[0], 'rb') as f:
uncompressed_data = f.read()
with open(simple_compressed_file[1], 'rb') as f:
compressed_data = f.read()
outdata = []
o = brotli.Decompressor()
for i in range(0, len(compressed_data)):
outdata.append(o.decompress(compressed_data[i:i+1]))
outdata.append(o.flush())
outdata.append(o.finish())
assert b''.join(outdata) == uncompressed_data
示例6: test_streaming_compression
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def test_streaming_compression(one_compressed_file,
chunk_size,
mode,
quality,
lgwin,
lgblock):
"""
Confirm that the streaming compressor works as expected.
"""
compressed_chunks = []
c = brotli.Compressor(
mode=mode, quality=quality, lgwin=lgwin, lgblock=lgblock
)
with open(one_compressed_file, 'rb') as f:
while True:
next_data = f.read(chunk_size)
if not next_data:
break
compressed_chunks.append(c.compress(next_data))
compressed_chunks.append(c.finish())
decompressed = brotli.decompress(b''.join(compressed_chunks))
with open(one_compressed_file, 'rb') as f:
assert decompressed == f.read()
示例7: guard_lottery
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def guard_lottery(self):
for k in range(1):
try:
response = await bilibili().guard_list()
json_response = response.json()
break
except Exception:
continue
else:
Printer().printer("連接艦長服務器失敗,嘗試從CDN拉取數據", "Error", "red")
response = await bilibili().guard_list_v2()
encrypt_content = response.json()['data']['room_info']['description'].replace('b6','').replace('</p>','').replace('<p>','')
json_response = json.loads(brotli.decompress(encrypt.decrypt(encrypt_content)).decode())
for i in range(0, len(json_response)):
GuardId = json_response[i]['Id']
if GuardId not in GuardLottery.had_gotted_guard and GuardId != 0:
GuardLottery.had_gotted_guard.append(GuardId)
OriginRoomId = json_response[i]['RoomId']
await self.guard_join(OriginRoomId, GuardId)
await asyncio.sleep(0.2)
示例8: __init__
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def __init__(self, reader=None):
if not haveBrotli:
raise ImportError("No module named brotli")
self.majorVersion = None
self.minorVersion = None
self.metaData = None
self.privData = None
if reader:
self.majorVersion = reader.majorVersion
self.minorVersion = reader.minorVersion
if reader.metaLength:
reader.file.seek(reader.metaOffset)
rawData = reader.file.read(reader.metaLength)
assert len(rawData) == reader.metaLength
data = brotli.decompress(rawData)
assert len(data) == reader.metaOrigLength
self.metaData = data
if reader.privLength:
reader.file.seek(reader.privOffset)
data = reader.file.read(reader.privLength)
assert len(data) == reader.privLength
self.privData = data
示例9: post_dissect
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def post_dissect(self, s):
if not conf.contribs["http"]["auto_compression"]:
return s
encodings = self._get_encodings()
# Un-chunkify
if "chunked" in encodings:
data = b""
while s:
length, _, body = s.partition(b"\r\n")
try:
length = int(length, 16)
except ValueError:
# Not a valid chunk. Ignore
break
else:
load = body[:length]
if body[length:length + 2] != b"\r\n":
# Invalid chunk. Ignore
break
s = body[length + 2:]
data += load
if not s:
s = data
# Decompress
try:
if "deflate" in encodings:
import zlib
s = zlib.decompress(s)
elif "gzip" in encodings:
s = gzip_decompress(s)
elif "compress" in encodings:
import lzw
s = lzw.decompress(s)
elif "br" in encodings and is_brotli_available:
s = brotli.decompress(s)
except Exception:
# Cannot decompress - probably incomplete data
pass
return s
示例10: decode_image_url
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def decode_image_url(data):
try:
data = base64.urlsafe_b64decode(data)
text = brotli.decompress(data).decode('utf-8')
return validate_image_url(json.loads(text))
except (Invalid, json.JSONDecodeError, brotli.error, UnicodeDecodeError) as ex:
raise ImageUrlDecodeError(str(ex)) from ex
示例11: decompress
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def decompress(content, encoding, filename='N/A'):
"""
Decompress file content.
Required:
content (bytes): a file to be compressed
encoding: None (no compression) or 'gzip' or 'br'
Optional:
filename (str:default:'N/A'): Used for debugging messages
Raises:
NotImplementedError if an unsupported codec is specified.
compression.EncodeError if the encoder has an issue
Return: decompressed content
"""
try:
encoding = (encoding or '').lower()
if encoding == '':
return content
elif len(content) == 0:
raise DecompressionError('File contains zero bytes: ' + str(filename))
elif encoding == 'gzip':
return gunzip(content)
elif encoding == 'br':
return brotli_decompress(content)
except DecompressionError as err:
print("Filename: " + str(filename))
raise
raise NotImplementedError(str(encoding) + ' is not currently supported. Supported Options: None, gzip')
示例12: brotli_decompress
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def brotli_decompress(content):
if len(content) == 0:
raise DecompressionError('File contains zero bytes.')
return brotli.decompress(content)
示例13: test_decompression
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def test_decompression(simple_compressed_file):
"""
Decompressing files returns their original form using decompress.
"""
with open(simple_compressed_file[0], 'rb') as f:
uncompressed_data = f.read()
with open(simple_compressed_file[1], 'rb') as f:
compressed_data = f.read()
assert brotli.decompress(compressed_data) == uncompressed_data
示例14: test_decompressobj
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def test_decompressobj(simple_compressed_file):
with open(simple_compressed_file[0], 'rb') as f:
uncompressed_data = f.read()
with open(simple_compressed_file[1], 'rb') as f:
compressed_data = f.read()
o = brotli.Decompressor()
data = o.decompress(compressed_data)
data += o.flush()
data += o.finish()
assert data == uncompressed_data
示例15: test_streaming_decompression_fails_properly_on_garbage
# 需要導入模塊: import brotli [as 別名]
# 或者: from brotli import decompress [as 別名]
def test_streaming_decompression_fails_properly_on_garbage(exception_cls):
"""
Garbage data properly fails decompression.
"""
o = brotli.Decompressor()
with pytest.raises(exception_cls):
o.decompress(b'some random garbage')