From 2eebe7d91ec6f9d52305a89e47ebb145a818768b Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 12 Jun 2014 23:09:56 -0700 Subject: [PATCH] Add timeouts to HTTP requests --- homeassistant/components/device_tracker.py | 2 +- homeassistant/components/downloader.py | 2 +- homeassistant/remote.py | 9 +++++++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/device_tracker.py b/homeassistant/components/device_tracker.py index 3b26f09fefcc..05e8a5e224be 100644 --- a/homeassistant/components/device_tracker.py +++ b/homeassistant/components/device_tracker.py @@ -489,7 +489,7 @@ class LuciDeviceScanner(object): """ Perform one JSON RPC operation. """ data = json.dumps({'method': method, 'params': args}) try: - res = requests.post(url, data=data, **kwargs) + res = requests.post(url, data=data, timeout=5, **kwargs) except requests.exceptions.Timeout: self.logger.exception("Connection to the router timed out") return diff --git a/homeassistant/components/downloader.py b/homeassistant/components/downloader.py index 1254d7db61c6..1f109ec8b7f2 100644 --- a/homeassistant/components/downloader.py +++ b/homeassistant/components/downloader.py @@ -60,7 +60,7 @@ def setup(hass, download_path): final_path = None - req = requests.get(url, stream=True) + req = requests.get(url, stream=True, timeout=10) if req.status_code == 200: filename = None diff --git a/homeassistant/remote.py b/homeassistant/remote.py index 3a2a5529dae8..7bf53e372812 100644 --- a/homeassistant/remote.py +++ b/homeassistant/remote.py @@ -74,14 +74,19 @@ class API(object): try: if method == METHOD_GET: - return requests.get(url, params=data) + return requests.get(url, params=data, timeout=5) else: - return requests.request(method, url, data=data) + return requests.request(method, url, data=data, timeout=5) except requests.exceptions.ConnectionError: logging.getLogger(__name__).exception("Error connecting to server") raise ha.HomeAssistantError("Error connecting to server") + except requests.exceptions.Timeout: + error = "Timeout when talking to {}".format(self.host) + logging.getLogger(__name__).exception(error) + raise ha.HomeAssistantError(error) + class HomeAssistant(ha.HomeAssistant): """ Home Assistant that forwards work. """