requests_simpletest_ethernet.py
1 import board 2 import busio 3 from digitalio import DigitalInOut 4 from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K 5 import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket 6 import adafruit_requests as requests 7 8 cs = DigitalInOut(board.D10) 9 spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) 10 11 # Initialize ethernet interface with DHCP 12 eth = WIZNET5K(spi_bus, cs) 13 14 # Initialize a requests object with a socket and ethernet interface 15 requests.set_socket(socket, eth) 16 17 TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" 18 JSON_GET_URL = "http://httpbin.org/get" 19 JSON_POST_URL = "http://httpbin.org/post" 20 21 attempts = 3 # Number of attempts to retry each request 22 failure_count = 0 23 response = None 24 25 print("Fetching text from %s" % TEXT_URL) 26 while not response: 27 try: 28 response = requests.get(TEXT_URL) 29 failure_count = 0 30 except AssertionError as error: 31 print("Request failed, retrying...\n", error) 32 failure_count += 1 33 if failure_count >= attempts: 34 raise AssertionError( 35 "Failed to resolve hostname, \ 36 please check your router's DNS configuration." 37 ) from error 38 continue 39 print("-" * 40) 40 41 print("Text Response: ", response.text) 42 print("-" * 40) 43 response.close() 44 response = None 45 46 print("Fetching JSON data from %s" % JSON_GET_URL) 47 while not response: 48 try: 49 response = requests.get(JSON_GET_URL) 50 failure_count = 0 51 except AssertionError as error: 52 print("Request failed, retrying...\n", error) 53 failure_count += 1 54 if failure_count >= attempts: 55 raise AssertionError( 56 "Failed to resolve hostname, \ 57 please check your router's DNS configuration." 58 ) from error 59 continue 60 print("-" * 40) 61 62 print("JSON Response: ", response.json()) 63 print("-" * 40) 64 response.close() 65 response = None 66 67 data = "31F" 68 print("POSTing data to {0}: {1}".format(JSON_POST_URL, data)) 69 while not response: 70 try: 71 response = requests.post(JSON_POST_URL, data=data) 72 failure_count = 0 73 except AssertionError as error: 74 print("Request failed, retrying...\n", error) 75 failure_count += 1 76 if failure_count >= attempts: 77 raise AssertionError( 78 "Failed to resolve hostname, \ 79 please check your router's DNS configuration." 80 ) from error 81 continue 82 print("-" * 40) 83 84 json_resp = response.json() 85 # Parse out the 'data' key from json_resp dict. 86 print("Data received from server:", json_resp["data"]) 87 print("-" * 40) 88 response.close() 89 response = None 90 91 json_data = {"Date": "July 25, 2019"} 92 print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data)) 93 while not response: 94 try: 95 response = requests.post(JSON_POST_URL, json=json_data) 96 failure_count = 0 97 except AssertionError as error: 98 print("Request failed, retrying...\n", error) 99 failure_count += 1 100 if failure_count >= attempts: 101 raise AssertionError( 102 "Failed to resolve hostname, \ 103 please check your router's DNS configuration." 104 ) from error 105 continue 106 print("-" * 40) 107 108 json_resp = response.json() 109 # Parse out the 'json' key from json_resp dict. 110 print("JSON Data received from server:", json_resp["json"]) 111 print("-" * 40) 112 response.close()