Skip to content

Commit b175c9b

Browse files
committed
added with statement, changed status_test to status_test_url
The variable is actually an http header status test so keeping it as url instead of json_url for that one is good.
1 parent 7e30fb6 commit b175c9b

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

examples/wifi/requests_wifi_status_codes.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def print_http_status(code, description):
2727
"""Returns HTTP status code and description"""
2828
if "100" <= code <= "103":
2929
print(f" | ✅ Status Test: {code} - {description}")
30-
elif "200" <= code <= "299":
30+
elif "200" == code:
31+
print(f" | 🆗 Status Test: {code} - {description}")
32+
elif "201" <= code <= "299":
3133
print(f" | ✅ Status Test: {code} - {description}")
3234
elif "300" <= code <= "600":
3335
print(f" | ❌ Status Test: {code} - {description}")
@@ -103,7 +105,7 @@ def print_http_status(code, description):
103105
}
104106

105107
JSON_GET_URL = "https://httpbin.org/get"
106-
STATUS_TEST = "https://httpbin.org/status/"
108+
STATUS_TEST_URL = "https://httpbin.org/status/"
107109

108110
print(f"\nConnecting to {ssid}...")
109111
print(f"Signal Strength: {rssi}")
@@ -118,33 +120,33 @@ def print_http_status(code, description):
118120
HEADERS = {"user-agent": "blinka/1.0.0"}
119121

120122
print(f" | GET JSON: {JSON_GET_URL}")
121-
response = requests.get(JSON_GET_URL, headers=HEADERS)
122-
123-
json_data = response.json()
124-
HEADERS = json_data["headers"]
125-
print(f" | User-Agent: {HEADERS['User-Agent']}")
126-
127-
# HTTP STATUS CODE TESTING
128-
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
129-
STATUS_CODE = str(response.status_code)
130-
STATUS_DESCRIPTION = http_status_codes.get(STATUS_CODE, "Unknown Status Code")
131-
print_http_status(STATUS_CODE, STATUS_DESCRIPTION)
132-
response.close()
133-
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
134-
print(" | ")
135-
136-
print(f" | Status Code Test: {STATUS_TEST}")
137-
# Some return errors then confirm the error (that's a good thing)
138-
# Demonstrates not all errors have the same behavior
139-
# 300, 304, and 306 in particular
140-
for codes in sorted(http_status_codes.keys(), key=int):
141-
status_test_url = STATUS_TEST + codes
142-
response = requests.get(status_test_url, headers=HEADERS)
143-
SORT_STATUS_CODE = str(response.status_code)
144-
SORT_STATUS_DESC = http_status_codes.get(SORT_STATUS_CODE, "Unknown Status Code")
145-
print_http_status(SORT_STATUS_CODE, SORT_STATUS_DESC)
123+
with requests.get(JSON_GET_URL, headers=HEADERS) as response:
124+
json_data = response.json()
125+
HEADERS = json_data["headers"]
126+
print(f" | User-Agent: {HEADERS['User-Agent']}")
127+
128+
# HTTP STATUS CODE TESTING
129+
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
130+
STATUS_CODE = str(response.status_code)
131+
STATUS_DESCRIPTION = http_status_codes.get(STATUS_CODE, "Unknown Status Code")
132+
print_http_status(STATUS_CODE, STATUS_DESCRIPTION)
146133
response.close()
147-
148-
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
149-
150-
print("Finished!")
134+
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
135+
print(" | ")
136+
137+
print(f" | Status Code Test: {STATUS_TEST_URL}")
138+
# Some return errors then confirm the error (that's a good thing)
139+
# Demonstrates not all errors have the same behavior
140+
# 300, 304, and 306 in particular
141+
for codes in sorted(http_status_codes.keys(), key=int):
142+
header_status_test_url = STATUS_TEST_URL + codes
143+
response = requests.get(header_status_test_url, headers=HEADERS)
144+
SORT_STATUS_CODE = str(response.status_code)
145+
SORT_STATUS_DESC = http_status_codes.get(
146+
SORT_STATUS_CODE, "Unknown Status Code"
147+
)
148+
print_http_status(SORT_STATUS_CODE, SORT_STATUS_DESC)
149+
150+
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
151+
152+
print("Finished!")

0 commit comments

Comments
 (0)