From 1d168992960cbcc64f12a97f42e0127df3a6a1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Ricart?= Date: Fri, 9 Mar 2018 22:45:50 +0100 Subject: [PATCH] try/except when post --- esp8266/main.py | 24 ++++++++++++++++++------ esp8266/uConfigParser.py | 4 ++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/esp8266/main.py b/esp8266/main.py index 4b1cf88..e0aa7d6 100644 --- a/esp8266/main.py +++ b/esp8266/main.py @@ -1,5 +1,5 @@ from machine import Pin -from urequests import get, post +import urequests as requests from time import sleep_ms from gc import collect from uConfigParser import ConfigParser @@ -18,14 +18,26 @@ while True: try: url = config[section]['url'] method = config[section]['method'] + except: print("error when read section %s in config file" % section) else: continue - data = {option:config[section][option] for option in config_obj.options(section) if not 'url' in option or not "method" in option} - if method == "POST": - post(url, **data) - elif method == "GET": - get(url, **data) + data = {option:config[section][option] for option in config_obj.options(section)} + del data['url'] + del data['method'] + try: + if method == "GET": + print('GET') + r = requests.get(url, data=data) + elif method == "POST": + print('POST') + r = requests.post(url, data=data) + except: + pass + else: + print("close") + r.close() + print("closed") pin_value = current_value collect() diff --git a/esp8266/uConfigParser.py b/esp8266/uConfigParser.py index 23ad026..c2cc80a 100644 --- a/esp8266/uConfigParser.py +++ b/esp8266/uConfigParser.py @@ -4,8 +4,8 @@ class ConfigParser: def sections(self): """Return a list of section names, excluding [DEFAULT]""" - dict_to_return = [section for section in self.config_dict.keys() if not section in "DEFAULT"] - return dict_to_return + to_return = [section for section in self.config_dict.keys() if not section in "DEFAULT"] + return to_return def add_section(self, section): """Create a new section in the configuration."""