|
18 | 18 | JSON_GET_URL = "http://httpbin.org/get" |
19 | 19 | JSON_POST_URL = "http://httpbin.org/post" |
20 | 20 |
|
| 21 | +attempts = 3 # Number of attempts to retry each request |
| 22 | +failure_count = 0 |
| 23 | +response = None |
| 24 | + |
21 | 25 | 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 |
23 | 37 | print('-'*40) |
24 | 38 |
|
25 | 39 | print("Text Response: ", response.text) |
26 | 40 | print('-'*40) |
27 | 41 | response.close() |
| 42 | +response = None |
28 | 43 |
|
29 | 44 | 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 |
31 | 56 | print('-'*40) |
32 | 57 |
|
33 | 58 | print("JSON Response: ", response.json()) |
34 | 59 | print('-'*40) |
35 | 60 | response.close() |
| 61 | +response = None |
36 | 62 |
|
37 | 63 | data = '31F' |
38 | 64 | 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 |
40 | 76 | print('-'*40) |
41 | 77 |
|
42 | 78 | json_resp = response.json() |
43 | 79 | # Parse out the 'data' key from json_resp dict. |
44 | 80 | print("Data received from server:", json_resp['data']) |
45 | 81 | print('-'*40) |
46 | 82 | response.close() |
| 83 | +response = None |
47 | 84 |
|
48 | 85 | json_data = {"Date" : "July 25, 2019"} |
49 | 86 | 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 |
51 | 98 | print('-'*40) |
52 | 99 |
|
53 | 100 | json_resp = response.json() |
|
0 commit comments