diff --git a/examples/requests_advanced_ethernet.py b/examples/requests_advanced_ethernet.py new file mode 100644 index 0000000..cb1d8b1 --- /dev/null +++ b/examples/requests_advanced_ethernet.py @@ -0,0 +1,53 @@ +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" + +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) +while not response: + try: + response = requests.get(JSON_GET_URL, headers=headers) + failure_count = 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() +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() diff --git a/examples/requests_simpletest_ethernet.py b/examples/requests_simpletest_ethernet.py new file mode 100644 index 0000000..7856355 --- /dev/null +++ b/examples/requests_simpletest_ethernet.py @@ -0,0 +1,104 @@ +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" + +attempts = 3 # Number of attempts to retry each request +failure_count = 0 +response = None + +print("Fetching text from %s"%TEXT_URL) +while not response: + try: + response = requests.get(TEXT_URL) + failure_count = 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) +while not response: + try: + response = requests.get(JSON_GET_URL) + failure_count = 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)) +while not response: + try: + response = requests.post(JSON_POST_URL, data=data) + failure_count = 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() +# Parse out the 'data' key from json_resp dict. +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)) +while not response: + try: + response = requests.post(JSON_POST_URL, json=json_data) + failure_count = 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() +# Parse out the 'json' key from json_resp dict. +print("JSON Data received from server:", json_resp['json']) +print('-'*40) +response.close()