Skip to content

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.

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.

1 participant