Skip to content

Commit

Permalink
fix: add timeout to requests to prevent hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
sarabveer committed Apr 14, 2024
1 parent 618ea57 commit 503b378
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Config settings can be provided by the config.ini file, or set as environment va
| `exit_on_html_error` | `True` | Any error retrieving tdata will cause an exit, mostly redundant with exit_on_auth_error |
| `clear_auth_token_on_html_error` | `True` | This is useful if you don't want to exit, but do want to get a new session if/when getting the stats fails |
| `sleep_before_exit` | `True` | If you want to sleep before exiting on errors, useful for Docker container when you have `restart = always` |
| `request_timeout` | `30` | Seconds to wait before request to fetch modem webpage/data times out |

### InfluxDB Config

Expand Down
1 change: 1 addition & 0 deletions config.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ exit_on_auth_error = True
exit_on_html_error = True
clear_auth_token_on_html_error = True
sleep_before_exit = True
request_timeout = 30

# SB8200 Only
modem_ssl = False
Expand Down
1 change: 1 addition & 0 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def get_config(config_path=None):
'exit_on_html_error': True,
'clear_auth_token_on_html_error': True,
'sleep_before_exit': True,
'request_timeout': 30,

# SB8200 Only
'modem_ssl': False,
Expand Down
9 changes: 6 additions & 3 deletions src/arris_stats_s33.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def get_credential(config):
url=url,
json=payload,
headers=headers,
verify=verify_ssl
verify=verify_ssl,
timeout=config['request_timeout']
)

if resp.status_code != 200:
Expand Down Expand Up @@ -84,7 +85,8 @@ def get_credential(config):
url=url,
json=payload,
headers=headers,
verify=verify_ssl
verify=verify_ssl,
timeout=config['request_timeout']
)

if resp.status_code != 200:
Expand Down Expand Up @@ -144,7 +146,8 @@ def get_json(config, credential):
url=url,
json=payload,
headers=headers,
verify=verify_ssl
verify=verify_ssl,
timeout=config['request_timeout']
)
if resp.status_code != 200:
logging.error('Error retreiving json from %s', url)
Expand Down
21 changes: 18 additions & 3 deletions src/arris_stats_sb8200.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ def get_credential(config):
# have to send as a cookie with subsequent requests
try:
if config['modem_new_auth']:
resp = requests.get(auth_url, headers={'Authorization': 'Basic ' + auth_hash}, verify=verify_ssl)
resp = requests.get(
auth_url,
headers={'Authorization': 'Basic ' + auth_hash},
verify=verify_ssl,
timeout=config['request_timeout']
)
cookie = resp.cookies['sessionId']
logging.debug('cookie: %s', cookie)
else:
resp = requests.get(auth_url, auth=(username, password), verify=verify_ssl)
resp = requests.get(
auth_url,
auth=(username, password),
verify=verify_ssl,
timeout=config['request_timeout']
)
cookie = None

if resp.status_code != 200:
Expand Down Expand Up @@ -99,7 +109,12 @@ def get_html(config, credential):
logging.info('Retreiving stats from %s', init_url)

try:
resp = requests.get(url, cookies=cookies, verify=verify_ssl)
resp = requests.get(
url,
cookies=cookies,
verify=verify_ssl,
timeout=config['request_timeout']
)
if resp.status_code != 200:
logging.error('Error retreiving html from %s', url)
logging.error('Status code: %s', resp.status_code)
Expand Down
9 changes: 7 additions & 2 deletions src/comcast_xb8_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ def get_credential(config):
}

try:
resp = requests.post(url, data=data, allow_redirects=False)
resp = requests.post(
url,
data=data,
allow_redirects=False,
timeout=config['request_timeout']
)
cookies = resp.cookies

if resp.status_code != 302:
Expand Down Expand Up @@ -52,7 +57,7 @@ def get_html(config, cookies):
logging.info('Retreiving stats from %s', url)

try:
resp = requests.get(url, cookies=cookies)
resp = requests.get(url, cookies=cookies, timeout=config['request_timeout'])
if resp.status_code != 200:
logging.error('Error retreiving html from %s', url)
logging.error('Status code: %s', resp.status_code)
Expand Down

0 comments on commit 503b378

Please sign in to comment.