Skip to content

Commit

Permalink
Merge pull request #1520 from ar4s/fix-frozen-requests
Browse files Browse the repository at this point in the history
Fixed frozen request when content-length equals 0
  • Loading branch information
ar4s committed Jun 24, 2015
2 parents ef1a710 + cb7347e commit 9cc4698
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/ralph/discovery/http.py
Expand Up @@ -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


Expand Down

0 comments on commit 9cc4698

Please sign in to comment.