Skip to content

Commit 0501bf5

Browse files
author
brentru
committed
add failure check in simpletest example eth., retries
1 parent 8157d5c commit 0501bf5

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

examples/requests_simpletest_ethernet.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,83 @@
1818
JSON_GET_URL = "http://httpbin.org/get"
1919
JSON_POST_URL = "http://httpbin.org/post"
2020

21+
attempts = 3 # Number of attempts to retry each request
22+
failure_count = 0
23+
response = None
24+
2125
print("Fetching text from %s"%TEXT_URL)
22-
response = requests.get(TEXT_URL)
26+
while not response:
27+
try:
28+
response = requests.get(TEXT_URL)
29+
attempts = 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
2337
print('-'*40)
2438

2539
print("Text Response: ", response.text)
2640
print('-'*40)
2741
response.close()
42+
response = None
2843

2944
print("Fetching JSON data from %s"%JSON_GET_URL)
30-
response = requests.get(JSON_GET_URL)
45+
while not response:
46+
try:
47+
response = requests.get(JSON_GET_URL)
48+
attempts = 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
3156
print('-'*40)
3257

3358
print("JSON Response: ", response.json())
3459
print('-'*40)
3560
response.close()
61+
response = None
3662

3763
data = '31F'
3864
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
39-
response = requests.post(JSON_POST_URL, data=data)
65+
while not response:
66+
try:
67+
response = requests.post(JSON_POST_URL, data=data)
68+
attempts = 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
4076
print('-'*40)
4177

4278
json_resp = response.json()
4379
# Parse out the 'data' key from json_resp dict.
4480
print("Data received from server:", json_resp['data'])
4581
print('-'*40)
4682
response.close()
83+
response = None
4784

4885
json_data = {"Date" : "July 25, 2019"}
4986
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
50-
response = requests.post(JSON_POST_URL, json=json_data)
87+
while not response:
88+
try:
89+
response = requests.post(JSON_POST_URL, json=json_data)
90+
attempts = 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
5198
print('-'*40)
5299

53100
json_resp = response.json()

0 commit comments

Comments
 (0)