From 7452b88562c25344ecf9e8eed9b13325d4aef4e6 Mon Sep 17 00:00:00 2001 From: ulrischa Date: Wed, 5 Aug 2020 10:43:54 +0200 Subject: [PATCH] Requests to luftdaten should be in try except Requests to luftdaten should be in try except block. If script runs in background and and network (e.g. DNS) error occured, the script stops running. --- examples/luftdaten.py | 60 ++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/examples/luftdaten.py b/examples/luftdaten.py index 84f1117..59f3865 100755 --- a/examples/luftdaten.py +++ b/examples/luftdaten.py @@ -118,33 +118,39 @@ def send_to_luftdaten(values, id): pm_values_json = [{"value_type": key, "value": val} for key, val in pm_values.items()] temp_values_json = [{"value_type": key, "value": val} for key, val in temp_values.items()] - resp_1 = requests.post( - "https://api.luftdaten.info/v1/push-sensor-data/", - json={ - "software_version": "enviro-plus 0.0.1", - "sensordatavalues": pm_values_json - }, - headers={ - "X-PIN": "1", - "X-Sensor": id, - "Content-Type": "application/json", - "cache-control": "no-cache" - } - ) - - resp_2 = requests.post( - "https://api.luftdaten.info/v1/push-sensor-data/", - json={ - "software_version": "enviro-plus 0.0.1", - "sensordatavalues": temp_values_json - }, - headers={ - "X-PIN": "11", - "X-Sensor": id, - "Content-Type": "application/json", - "cache-control": "no-cache" - } - ) + try: + resp_1 = requests.post( + "https://api.luftdaten.info/v1/push-sensor-data/", + json={ + "software_version": "enviro-plus 0.0.1", + "sensordatavalues": pm_values_json + }, + headers={ + "X-PIN": "1", + "X-Sensor": id, + "Content-Type": "application/json", + "cache-control": "no-cache" + } + ) + except: + pass + + try: + resp_2 = requests.post( + "https://api.luftdaten.info/v1/push-sensor-data/", + json={ + "software_version": "enviro-plus 0.0.1", + "sensordatavalues": temp_values_json + }, + headers={ + "X-PIN": "11", + "X-Sensor": id, + "Content-Type": "application/json", + "cache-control": "no-cache" + } + ) + except: + pass if resp_1.ok and resp_2.ok: return True