-
Notifications
You must be signed in to change notification settings - Fork 577
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
base: master
Are you sure you want to change the base?
Conversation
@@ -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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
- A shim on the request: Feature Request: Add timeout to session psf/requests#2011 (comment)
- in intelmq we have our own TimeoutHTTPAdapter https://github.com/certtools/intelmq/blob/develop/intelmq/lib/utils.py#L692-L726
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: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 theShodan
class.