Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout to requests in Shodan._request() #111

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shodan/cli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
'org': 'cyan',
'vulns': 'red',
}
REQUESTS_TIMEOUT = (30, 30) # (connect, read)
9 changes: 5 additions & 4 deletions shodan/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to set the timeout on the session, that's easier (self._session.timeout = 30 or similar)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I thought about speficying the timeout directly when the session object is created but it's no supported and there's no setter for it. I didn't think about assigning directly to the attribute. I'll do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, it doesn't work. The session object does not have a timeout attribute at all. The timeout is directly passed to the method.

https://github.com/psf/requests/blob/09fd857eb1448a8234e39bc39e2c795d4c980bfb/requests/sessions.py#L337

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, ok that seems to have worked previously, but not anymore.

Other solutions:

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')

Expand Down