From e5470f49c448e6c21ea8c777fc20072b2855fb89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Ribeiro?= Date: Sun, 3 Nov 2019 23:03:13 +0000 Subject: [PATCH 1/2] Define REQUESTS_TIMEOUT constant --- shodan/cli/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/shodan/cli/settings.py b/shodan/cli/settings.py index 6f27e5d..30291a5 100644 --- a/shodan/cli/settings.py +++ b/shodan/cli/settings.py @@ -8,3 +8,4 @@ 'org': 'cyan', 'vulns': 'red', } +REQUESTS_TIMEOUT = (30, 30) # (connect, read) From ee1278792f6e418a5262746ef3b5dec792ddd94f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Ribeiro?= Date: Sun, 3 Nov 2019 23:06:18 +0000 Subject: [PATCH 2/2] Add timeout to session calls in Shodan._request() --- shodan/client.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/shodan/client.py b/shodan/client.py index 6e66259..4581806 100644 --- a/shodan/client.py +++ b/shodan/client.py @@ -15,6 +15,7 @@ from .exception import APIError from .helpers import api_request, create_facet_string from .stream import Stream +from .cli.settings import REQUESTS_TIMEOUT # Try to disable the SSL warnings in urllib3 since not everybody can install @@ -294,13 +295,13 @@ def _request(self, function, params, service='shodan', method='get'): try: method = method.lower() if method == 'post': - data = self._session.post(base_url + function, params) + data = self._session.post(base_url + function, params, timeout=REQUESTS_TIMEOUT) elif method == 'put': - data = self._session.put(base_url + function, params=params) + data = self._session.put(base_url + function, params=params, timeout=REQUESTS_TIMEOUT) elif method == 'delete': - data = self._session.delete(base_url + function, params=params) + data = self._session.delete(base_url + function, params=params, timeout=REQUESTS_TIMEOUT) else: - data = self._session.get(base_url + function, params=params) + data = self._session.get(base_url + function, params=params, timeout=REQUESTS_TIMEOUT) except Exception: raise APIError('Unable to connect to Shodan')