Skip to content

Commit 4f88beb

Browse files
authored
Merge pull request #176 from DJDevon3/DJDevon3-MultipleCookies
Update multiple cookies example
2 parents d595686 + 240a858 commit 4f88beb

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed
Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
# SPDX-FileCopyrightText: 2022 Alec Delaney
22
# SPDX-License-Identifier: MIT
3-
4-
"""
5-
This example was written for the MagTag; changes may be needed
6-
for connecting to the internet depending on your device.
7-
"""
3+
# Coded for Circuit Python 9.0
4+
""" Multiple Cookies Example written for MagTag """
85

96
import os
10-
import ssl
117

12-
import socketpool
8+
import adafruit_connection_manager
139
import wifi
1410

1511
import adafruit_requests
1612

17-
COOKIE_TEST_URL = "https://www.adafruit.com"
18-
1913
# Get WiFi details, ensure these are setup in settings.toml
2014
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
2115
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
2216

23-
# Connect to the Wi-Fi network
24-
print("Connecting to %s" % ssid)
25-
wifi.radio.connect(ssid, password)
17+
COOKIE_TEST_URL = "https://www.adafruit.com"
2618

27-
# Set up the requests library
28-
pool = socketpool.SocketPool(wifi.radio)
29-
requests = adafruit_requests.Session(pool, ssl.create_default_context())
19+
# Initalize Wifi, Socket Pool, Request Session
20+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
21+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
22+
requests = adafruit_requests.Session(pool, ssl_context)
3023

31-
# GET from the URL
32-
print("Fetching multiple cookies from", COOKIE_TEST_URL)
33-
response = requests.get(COOKIE_TEST_URL)
24+
print(f"\nConnecting to {ssid}...")
25+
try:
26+
# Connect to the Wi-Fi network
27+
wifi.radio.connect(ssid, password)
28+
# URL GET Request
29+
response = requests.get(COOKIE_TEST_URL)
30+
except OSError as e:
31+
print(f"❌ OSError: {e}")
32+
print("✅ Wifi!")
33+
34+
print(f" | Fetching Cookies: {COOKIE_TEST_URL}")
3435

3536
# Spilt up the cookies by ", "
3637
elements = response.headers["set-cookie"].split(", ")
@@ -49,10 +50,13 @@
4950
cookie_list.append(element)
5051

5152
# Pring the information about the cookies
52-
print("Number of cookies:", len(cookie_list))
53-
print("")
54-
print("Cookies received:")
55-
print("-" * 40)
53+
print(f" | Total Cookies: {len(cookie_list)}")
54+
print("-" * 80)
55+
5656
for cookie in cookie_list:
57-
print(cookie)
58-
print("-" * 40)
57+
print(f" | 🍪 {cookie}")
58+
print("-" * 80)
59+
60+
response.close()
61+
print(f"✂️ Disconnected from {COOKIE_TEST_URL}")
62+
print("Finished!")

0 commit comments

Comments
 (0)