From cb7347e03bc94594ebfa0caf16bc55f88bc88d20 Mon Sep 17 00:00:00 2001 From: Arkadiusz Adamski Date: Tue, 23 Jun 2015 16:51:00 +0200 Subject: [PATCH] fixed frozen request when content-length equals 0 Prevent freezing request in ``get_http_info``. --- src/ralph/discovery/http.py | 38 ++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/ralph/discovery/http.py b/src/ralph/discovery/http.py index ab5873e898..09a42fe6e3 100644 --- a/src/ralph/discovery/http.py +++ b/src/ralph/discovery/http.py @@ -20,29 +20,45 @@ 'IBM_HTTP_Server': 'IBM', '': 'Unspecified', } -REQUEST_TIMEOUT = 5 +REQUEST_KWARGS = { + 'timeout': 5, + 'verify': False, + 'allow_redirects': False, +} logger = logging.getLogger(__name__) +def http_get_method(session, url): + """ + Handle redirects by manual way without fetching content + (forced by stream=True) to avoid hanged request when content length is 0. + """ + response = session.get(url, stream=True, **REQUEST_KWARGS) + if response.status_code in (301, 302) and response.headers.get('location'): + return http_get_method(session, response.headers['location']) + return response + + def get_http_info(ip): headers, content = {}, '' url = "http://{}".format(ip) - try: - response = requests.get(url, timeout=REQUEST_TIMEOUT, verify=False) - response.raise_for_status() - headers, content = response.headers, response.content - except requests.exceptions.HTTPError as e: - logger.error("HTTPERROR: {} for url: {}".format(e, url)) - except requests.exceptions.RequestException as e: - logger.error("Exception: {} for url: {}".format(e, url)) - url = "https://{}".format(':'.join((ip, '8006'))) # for Proxmox + with requests.Session() as session: try: - response = requests.get(url, timeout=REQUEST_TIMEOUT, verify=False) + response = http_get_method(session, url) response.raise_for_status() headers, content = response.headers, response.content + except requests.exceptions.HTTPError as e: + logger.error("HTTPERROR: {} for url: {}".format(e, url)) except requests.exceptions.RequestException as e: logger.error("Exception: {} for url: {}".format(e, url)) + url = "https://{}".format(':'.join((ip, '8006'))) # for Proxmox + try: + response = http_get_method(session, url) + response.raise_for_status() + headers, content = response.headers, response.content + except requests.exceptions.RequestException as e: + logger.error("Exception: {} for url: {}".format(e, url)) return headers, content