Skip to content

Commit

Permalink
Add timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
annndruha committed Mar 12, 2024
1 parent 15954a6 commit dc69c82
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions wifi_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@


def keen_auth(username, password):
r = session.get(f'{WIFI_HOST}/auth')
r = session.get(f'{WIFI_HOST}/auth',
timeout=10)
if r.status_code == 401:
md5 = username + ':' + r.headers['X-NDM-Realm'] + ':' + password
md5 = hashlib.md5(md5.encode('utf-8'))
sha = r.headers['X-NDM-Challenge'] + md5.hexdigest()
sha = hashlib.sha256(sha.encode('utf-8'))
r_new = session.post(f'{WIFI_HOST}/auth', json={'login': username, 'password': sha.hexdigest()})
r_new = session.post(f'{WIFI_HOST}/auth', json={'login': username, 'password': sha.hexdigest()},
timeout=10)
if r_new.status_code == 200:
return True
elif r.status_code == 200:
Expand All @@ -48,7 +50,8 @@ def keen_auth(username, password):


def update_clients():
r = session.get(f'{WIFI_HOST}/rci/show/ip/hotspot')
r = session.get(f'{WIFI_HOST}/rci/show/ip/hotspot',
timeout=10)
if r.status_code != 200:
raise ConnectionRefusedError(f'Status code: {r.status_code}')

Expand Down Expand Up @@ -86,7 +89,8 @@ def send_message(message):
f'/sendMessage'
f'?chat_id={TG_CHAT_ID}'
f'&parse_mode=HTML'
f'&text={message}')
f'&text={message}',
timeout=10)
if r.status_code != 200:
logging.error(f'[Telegram error] {r.status_code}, {r.text}')

Expand All @@ -96,11 +100,13 @@ def send_host_down_message(error):
At first glance, it seems that this function can be removed,
but otherwise unable to send a message with a quote
"""

def utf16len(text):
i = 0
for c in text:
i += 1 if ord(c) < 65536 else 2
return i

unquoted_text = f'🔥 {WIFI_NAME} host probably down.'
offset = utf16len(unquoted_text)
length = utf16len(str(error))
Expand All @@ -112,7 +118,8 @@ def utf16len(text):
'entities': json.dumps([{'type': 'blockquote',
'offset': offset,
'length': length}])}
r = requests.post(f'https://api.telegram.org/bot{TG_BOT_TOKEN}/sendMessage', data=body)
r = requests.post(f'https://api.telegram.org/bot{TG_BOT_TOKEN}/sendMessage', data=body,
timeout=10)
if r.status_code != 200:
logging.error(f'[Telegram error] {r.status_code}, {r.text}')

Expand Down

0 comments on commit dc69c82

Please sign in to comment.