Skip to content

Commit 08422e8

Browse files
committed
Update multiple cookies example
Example for requesting cookies in a GET request. Updated to Connection Manager for 9.0
1 parent fdf4cbf commit 08422e8

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed
Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
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 """
5+
# pylint: disable=import-error
86

97
import os
10-
import ssl
118

12-
import socketpool
9+
import adafruit_connection_manager
1310
import wifi
1411

1512
import adafruit_requests
1613

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

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

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

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

3537
# Spilt up the cookies by ", "
3638
elements = response.headers["set-cookie"].split(", ")
@@ -49,10 +51,13 @@
4951
cookie_list.append(element)
5052

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

0 commit comments

Comments
 (0)