From 1b5cfc2ec0c1be8bb12c8d291fe5981394515374 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 6 Mar 2020 11:35:55 -0500 Subject: [PATCH 1/5] eth simpletest for guide --- examples/requests_simpletest_ethernet.py | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/requests_simpletest_ethernet.py diff --git a/examples/requests_simpletest_ethernet.py b/examples/requests_simpletest_ethernet.py new file mode 100644 index 0000000..c405d87 --- /dev/null +++ b/examples/requests_simpletest_ethernet.py @@ -0,0 +1,57 @@ +import board +import busio +from digitalio import DigitalInOut +from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K +import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket +import adafruit_requests as requests + +cs = DigitalInOut(board.D10) +spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) + +# Initialize ethernet interface with DHCP +eth = WIZNET5K(spi_bus, cs) + +# Initialize a requests object with a socket and ethernet interface +requests.set_socket(socket, eth) + +TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" +JSON_GET_URL = "http://httpbin.org/get" +JSON_POST_URL = "http://httpbin.org/post" + +print("Fetching text from %s"%TEXT_URL) +response = requests.get(TEXT_URL) +print('-'*40) + +print("Text Response: ", response.text) +print('-'*40) +response.close() + +print("Fetching JSON data from %s"%JSON_GET_URL) +response = requests.get(JSON_GET_URL) +print('-'*40) + +print("JSON Response: ", response.json()) +print('-'*40) +response.close() + +data = '31F' +print("POSTing data to {0}: {1}".format(JSON_POST_URL, data)) +response = requests.post(JSON_POST_URL, data=data) +print('-'*40) + +json_resp = response.json() +# Parse out the 'data' key from json_resp dict. +print("Data received from server:", json_resp['data']) +print('-'*40) +response.close() + +json_data = {"Date" : "July 25, 2019"} +print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data)) +response = requests.post(JSON_POST_URL, json=json_data) +print('-'*40) + +json_resp = response.json() +# Parse out the 'json' key from json_resp dict. +print("JSON Data received from server:", json_resp['json']) +print('-'*40) +response.close() From 8157d5c36ce6268d49303880ec0ae095bc8008a0 Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 6 Mar 2020 11:42:32 -0500 Subject: [PATCH 2/5] add advanced usage for ethernet interface --- examples/requests_advanced_ethernet.py | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/requests_advanced_ethernet.py diff --git a/examples/requests_advanced_ethernet.py b/examples/requests_advanced_ethernet.py new file mode 100644 index 0000000..3831bd0 --- /dev/null +++ b/examples/requests_advanced_ethernet.py @@ -0,0 +1,39 @@ +import board +import busio +from digitalio import DigitalInOut +from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K +import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket +import adafruit_requests as requests + +cs = DigitalInOut(board.D10) +spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) + +# Initialize ethernet interface with DHCP +eth = WIZNET5K(spi_bus, cs) + +# Initialize a requests object with a socket and ethernet interface +requests.set_socket(socket, eth) + +JSON_GET_URL = "http://httpbin.org/get" + +# Define a custom header as a dict. +headers = {"user-agent" : "blinka/1.0.0"} + +print("Fetching JSON data from %s..."%JSON_GET_URL) +response = requests.get(JSON_GET_URL, headers=headers) +print('-'*60) + +json_data = response.json() +headers = json_data['headers'] +print("Response's Custom User-Agent Header: {0}".format(headers['User-Agent'])) +print('-'*60) + +# Read Response's HTTP status code +print("Response HTTP Status Code: ", response.status_code) +print('-'*60) + +# Read Response, as raw bytes instead of pretty text +print("Raw Response: ", response.content) + +# Close, delete and collect the response data +response.close() From 0501bf51157786a3d23c383e0af5d6d45b6f94fa Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 10 Mar 2020 14:34:12 -0400 Subject: [PATCH 3/5] add failure check in simpletest example eth., retries --- examples/requests_simpletest_ethernet.py | 55 ++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/examples/requests_simpletest_ethernet.py b/examples/requests_simpletest_ethernet.py index c405d87..1ca4105 100644 --- a/examples/requests_simpletest_ethernet.py +++ b/examples/requests_simpletest_ethernet.py @@ -18,25 +18,61 @@ JSON_GET_URL = "http://httpbin.org/get" JSON_POST_URL = "http://httpbin.org/post" +attempts = 3 # Number of attempts to retry each request +failure_count = 0 +response = None + print("Fetching text from %s"%TEXT_URL) -response = requests.get(TEXT_URL) +while not response: + try: + response = requests.get(TEXT_URL) + attempts = 0 + except AssertionError as error: + print("Request failed, retrying...\n", error) + failure_count += 1 + if failure_count >= attempts: + raise AssertionError("Failed to resolve hostname, \ + please check your router's DNS configuration.") + continue print('-'*40) print("Text Response: ", response.text) print('-'*40) response.close() +response = None print("Fetching JSON data from %s"%JSON_GET_URL) -response = requests.get(JSON_GET_URL) +while not response: + try: + response = requests.get(JSON_GET_URL) + attempts = 0 + except AssertionError as error: + print("Request failed, retrying...\n", error) + failure_count += 1 + if failure_count >= attempts: + raise AssertionError("Failed to resolve hostname, \ + please check your router's DNS configuration.") + continue print('-'*40) print("JSON Response: ", response.json()) print('-'*40) response.close() +response = None data = '31F' print("POSTing data to {0}: {1}".format(JSON_POST_URL, data)) -response = requests.post(JSON_POST_URL, data=data) +while not response: + try: + response = requests.post(JSON_POST_URL, data=data) + attempts = 0 + except AssertionError as error: + print("Request failed, retrying...\n", error) + failure_count += 1 + if failure_count >= attempts: + raise AssertionError("Failed to resolve hostname, \ + please check your router's DNS configuration.") + continue print('-'*40) json_resp = response.json() @@ -44,10 +80,21 @@ print("Data received from server:", json_resp['data']) print('-'*40) response.close() +response = None json_data = {"Date" : "July 25, 2019"} print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data)) -response = requests.post(JSON_POST_URL, json=json_data) +while not response: + try: + response = requests.post(JSON_POST_URL, json=json_data) + attempts = 0 + except AssertionError as error: + print("Request failed, retrying...\n", error) + failure_count += 1 + if failure_count >= attempts: + raise AssertionError("Failed to resolve hostname, \ + please check your router's DNS configuration.") + continue print('-'*40) json_resp = response.json() From 79ac648180af459a2d9a68cfc47d2640736c9889 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 10 Mar 2020 14:35:17 -0400 Subject: [PATCH 4/5] add advanced test --- examples/requests_advanced_ethernet.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/requests_advanced_ethernet.py b/examples/requests_advanced_ethernet.py index 3831bd0..cfd6e6d 100644 --- a/examples/requests_advanced_ethernet.py +++ b/examples/requests_advanced_ethernet.py @@ -16,11 +16,25 @@ JSON_GET_URL = "http://httpbin.org/get" +attempts = 3 # Number of attempts to retry each request +failure_count = 0 +response = None + # Define a custom header as a dict. headers = {"user-agent" : "blinka/1.0.0"} print("Fetching JSON data from %s..."%JSON_GET_URL) -response = requests.get(JSON_GET_URL, headers=headers) +while not response: + try: + response = requests.get(JSON_GET_URL, headers=headers) + attempts = 0 + except AssertionError as error: + print("Request failed, retrying...\n", error) + failure_count += 1 + if failure_count >= attempts: + raise AssertionError("Failed to resolve hostname, \ + please check your router's DNS configuration.") + continue print('-'*60) json_data = response.json() From b5319421e9d593d9c3589ee6c5b35b9716eb0605 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 11 Mar 2020 09:56:25 -0400 Subject: [PATCH 5/5] switch reset to failure_count --- examples/requests_advanced_ethernet.py | 2 +- examples/requests_simpletest_ethernet.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/requests_advanced_ethernet.py b/examples/requests_advanced_ethernet.py index cfd6e6d..cb1d8b1 100644 --- a/examples/requests_advanced_ethernet.py +++ b/examples/requests_advanced_ethernet.py @@ -27,7 +27,7 @@ while not response: try: response = requests.get(JSON_GET_URL, headers=headers) - attempts = 0 + failure_count = 0 except AssertionError as error: print("Request failed, retrying...\n", error) failure_count += 1 diff --git a/examples/requests_simpletest_ethernet.py b/examples/requests_simpletest_ethernet.py index 1ca4105..7856355 100644 --- a/examples/requests_simpletest_ethernet.py +++ b/examples/requests_simpletest_ethernet.py @@ -26,7 +26,7 @@ while not response: try: response = requests.get(TEXT_URL) - attempts = 0 + failure_count = 0 except AssertionError as error: print("Request failed, retrying...\n", error) failure_count += 1 @@ -45,7 +45,7 @@ while not response: try: response = requests.get(JSON_GET_URL) - attempts = 0 + failure_count = 0 except AssertionError as error: print("Request failed, retrying...\n", error) failure_count += 1 @@ -65,7 +65,7 @@ while not response: try: response = requests.post(JSON_POST_URL, data=data) - attempts = 0 + failure_count = 0 except AssertionError as error: print("Request failed, retrying...\n", error) failure_count += 1 @@ -87,7 +87,7 @@ while not response: try: response = requests.post(JSON_POST_URL, json=json_data) - attempts = 0 + failure_count = 0 except AssertionError as error: print("Request failed, retrying...\n", error) failure_count += 1