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

Conversation

goncalor
Copy link
Contributor

@goncalor goncalor commented Nov 3, 2019

I've noticed that when facing network problems (e.g. Wi-Fi connection dropped) Shodan may hang indefinitely without timing out. The code calling the Shodan library has no control over this and may be left hanging for a response.

According to the requests documentation requests should have a timeout:

Most requests to external servers should have a timeout attached, in case the server is not responding in a timely manner. By default, requests do not time out unless a timeout value is set explicitly. Without a timeout, your code may hang for minutes or more.

These changes define a REQUESTS_TIMEOUT constant which defines a connect and read timeout of 30 seconds each. These timeouts are applied to the _request() method of the Shodan class.

@@ -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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant