Skip to content

Commit 833e46d

Browse files
authored
Merge pull request #22 from brentru/update-examples-for-ethernet
Add Simpletest for Ethernet Library
2 parents 327d2e4 + b531942 commit 833e46d

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
JSON_GET_URL = "http://httpbin.org/get"
18+
19+
attempts = 3 # Number of attempts to retry each request
20+
failure_count = 0
21+
response = None
22+
23+
# Define a custom header as a dict.
24+
headers = {"user-agent" : "blinka/1.0.0"}
25+
26+
print("Fetching JSON data from %s..."%JSON_GET_URL)
27+
while not response:
28+
try:
29+
response = requests.get(JSON_GET_URL, headers=headers)
30+
failure_count = 0
31+
except AssertionError as error:
32+
print("Request failed, retrying...\n", error)
33+
failure_count += 1
34+
if failure_count >= attempts:
35+
raise AssertionError("Failed to resolve hostname, \
36+
please check your router's DNS configuration.")
37+
continue
38+
print('-'*60)
39+
40+
json_data = response.json()
41+
headers = json_data['headers']
42+
print("Response's Custom User-Agent Header: {0}".format(headers['User-Agent']))
43+
print('-'*60)
44+
45+
# Read Response's HTTP status code
46+
print("Response HTTP Status Code: ", response.status_code)
47+
print('-'*60)
48+
49+
# Read Response, as raw bytes instead of pretty text
50+
print("Raw Response: ", response.content)
51+
52+
# Close, delete and collect the response data
53+
response.close()
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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("Failed to resolve hostname, \
35+
please check your router's DNS configuration.")
36+
continue
37+
print('-'*40)
38+
39+
print("Text Response: ", response.text)
40+
print('-'*40)
41+
response.close()
42+
response = None
43+
44+
print("Fetching JSON data from %s"%JSON_GET_URL)
45+
while not response:
46+
try:
47+
response = requests.get(JSON_GET_URL)
48+
failure_count = 0
49+
except AssertionError as error:
50+
print("Request failed, retrying...\n", error)
51+
failure_count += 1
52+
if failure_count >= attempts:
53+
raise AssertionError("Failed to resolve hostname, \
54+
please check your router's DNS configuration.")
55+
continue
56+
print('-'*40)
57+
58+
print("JSON Response: ", response.json())
59+
print('-'*40)
60+
response.close()
61+
response = None
62+
63+
data = '31F'
64+
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
65+
while not response:
66+
try:
67+
response = requests.post(JSON_POST_URL, data=data)
68+
failure_count = 0
69+
except AssertionError as error:
70+
print("Request failed, retrying...\n", error)
71+
failure_count += 1
72+
if failure_count >= attempts:
73+
raise AssertionError("Failed to resolve hostname, \
74+
please check your router's DNS configuration.")
75+
continue
76+
print('-'*40)
77+
78+
json_resp = response.json()
79+
# Parse out the 'data' key from json_resp dict.
80+
print("Data received from server:", json_resp['data'])
81+
print('-'*40)
82+
response.close()
83+
response = None
84+
85+
json_data = {"Date" : "July 25, 2019"}
86+
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
87+
while not response:
88+
try:
89+
response = requests.post(JSON_POST_URL, json=json_data)
90+
failure_count = 0
91+
except AssertionError as error:
92+
print("Request failed, retrying...\n", error)
93+
failure_count += 1
94+
if failure_count >= attempts:
95+
raise AssertionError("Failed to resolve hostname, \
96+
please check your router's DNS configuration.")
97+
continue
98+
print('-'*40)
99+
100+
json_resp = response.json()
101+
# Parse out the 'json' key from json_resp dict.
102+
print("JSON Data received from server:", json_resp['json'])
103+
print('-'*40)
104+
response.close()

0 commit comments

Comments
 (0)