chg: Keep connection alive between requests

pull/649/head
Jakub Onderka 2020-10-29 13:40:23 +01:00
parent e10843fa33
commit 9aa119e080
1 changed files with 16 additions and 14 deletions

View File

@ -114,6 +114,7 @@ class PyMISP:
self.auth: Optional[AuthBase] = auth self.auth: Optional[AuthBase] = auth
self.tool: str = tool self.tool: str = tool
self.timeout: Optional[Union[float, Tuple[float, float]]] = timeout self.timeout: Optional[Union[float, Tuple[float, float]]] = timeout
self.__session = requests.Session() # use one session to keep connection between requests
self.global_pythonify = False self.global_pythonify = False
@ -3005,21 +3006,22 @@ class PyMISP:
# CakePHP params in URL # CakePHP params in URL
to_append_url = '/'.join([f'{k}:{v}' for k, v in kw_params.items()]) to_append_url = '/'.join([f'{k}:{v}' for k, v in kw_params.items()])
url = f'{url}/{to_append_url}' url = f'{url}/{to_append_url}'
req = requests.Request(request_type, url, data=d, params=params) req = requests.Request(request_type, url, data=d, params=params)
with requests.Session() as s:
user_agent = f'PyMISP {__version__} - Python {".".join(str(x) for x in sys.version_info[:2])}' user_agent = f'PyMISP {__version__} - Python {".".join(str(x) for x in sys.version_info[:2])}'
if self.tool: if self.tool:
user_agent = f'{user_agent} - {self.tool}' user_agent = f'{user_agent} - {self.tool}'
req.auth = self.auth req.auth = self.auth
prepped = s.prepare_request(req) prepped = self.__session.prepare_request(req)
prepped.headers.update( prepped.headers.update(
{'Authorization': self.key, {'Authorization': self.key,
'Accept': f'application/{output_type}', 'Accept': f'application/{output_type}',
'content-type': 'application/json', 'content-type': 'application/json',
'User-Agent': user_agent}) 'User-Agent': user_agent})
logger.debug(prepped.headers) logger.debug(prepped.headers)
settings = s.merge_environment_settings(req.url, proxies=self.proxies or {}, stream=None, verify=self.ssl, cert=self.cert) settings = self.__session.merge_environment_settings(req.url, proxies=self.proxies or {}, stream=None,
return s.send(prepped, timeout=self.timeout, **settings) verify=self.ssl, cert=self.cert)
return self.__session.send(prepped, timeout=self.timeout, **settings)
def _csv_to_dict(self, csv_content: str) -> List[dict]: def _csv_to_dict(self, csv_content: str) -> List[dict]:
'''Makes a list of dict out of a csv file (requires headers)''' '''Makes a list of dict out of a csv file (requires headers)'''