new: Support brotli compression

pull/681/head
Jakub Onderka 2021-01-15 20:19:19 +01:00
parent de6125a623
commit 361d8d0944
1 changed files with 24 additions and 0 deletions

View File

@ -15,6 +15,7 @@ from uuid import UUID
import warnings
import sys
import copy
import urllib3
from io import BytesIO, StringIO
from . import __version__, everything_broken
@ -87,6 +88,27 @@ def register_user(misp_url: str, email: str,
return r.json()
def brotli_supported() -> bool:
"""
Returns whether Brotli compression is supported
"""
# urllib >= 1.25.1 includes brotli support
major, minor, patch = urllib3.__version__.split('.') # noqa: F811
major, minor, patch = int(major), int(minor), int(patch)
urllib3_with_brotli = (major == 1 and ((minor == 25 and patch >= 1) or (minor >= 26))) or major >= 2
if not urllib3_with_brotli:
return False
# pybrotli is an extra package required by urllib3 for brotli support
try:
import brotli
return True
except ImportError:
return False
class PyMISP:
"""Python API for MISP
@ -117,6 +139,8 @@ class PyMISP:
self.tool: str = tool
self.timeout: Optional[Union[float, Tuple[float, float]]] = timeout
self.__session = requests.Session() # use one session to keep connection between requests
if brotli_supported():
self.__session.headers['Accept-Encoding'] = ', '.join(('br', 'gzip', 'deflate'))
self.global_pythonify = False