GZIP¶
Overview¶
The different and benchmark: https://www.coralnodes.com/gzip-vs-brotli/
Build from Google https://github.com/google/brotli
can from pip
Implement brotli request
from https://stackoverflow.com/questions/63809144/python3-8-brotli-brotli-error-brotlidecompress-failed
# This is mentioned nowhere in the documentation of requests but once brotli is installed, it is directly handled by Requests.
# This means that response.content will be automatically decoded (similarly to gzip). You don't need to do brotli.decompress(response.content)
# if brotli is not installed, you won't get any error message. Instead, response.content will stay encoded...
# edit:
# digging in Requests code, I found out that Requests use urllib3.response which implements usage of Brotli.
# upon loading, urllib3.response will look for an import of Brotli:
# thus if Brotli is installed, decoding will occur else nothing will happen and no warning to the user.
# edit2 In fact, this is mentioned in <https://docs.python-requests.org/en/latest/user/quickstart/#binary-response-content>
try:
import brotli
except ImportError:
brotli = None
then when decoding a response, it will use appropriate decoder:
def _get_decoder(mode):
if "," in mode:
return MultiDecoder(mode)
if mode == "gzip":
return GzipDecoder()
if brotli is not None and mode == "br":
return BrotliDecoder()
return DeflateDecoder()
Using request
with birary from https://docs.python-requests.org/en/latest/user/quickstart/#binary-response-content
# Binary Response Content
# You can also access the response body as bytes, for non-text requests:
# r.content
# b'[{"repository":{"open_issues":0,"url":"https://github.com/...
# The gzip and deflate transfer-encodings are automatically decoded for you.
# The br transfer-encoding is automatically decoded for you if a Brotli library like brotli or brotlicffi is installed.
# For example, to create an image from binary data returned by a request, you can use the following code:
# from PIL import Image
# from io import BytesIO
# i = Image.open(BytesIO(r.content))
with FastAPI framework https://github.com/fullonic/brotli-asgi