|
1 | 1 | # SPDX-FileCopyrightText: 2022 Alec Delaney |
2 | 2 | # 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 """ |
8 | 5 |
|
9 | 6 | import os |
10 | | -import ssl |
11 | 7 |
|
12 | | -import socketpool |
| 8 | +import adafruit_connection_manager |
13 | 9 | import wifi |
14 | 10 |
|
15 | 11 | import adafruit_requests |
16 | 12 |
|
17 | | -COOKIE_TEST_URL = "https://www.adafruit.com" |
18 | | - |
19 | 13 | # Get WiFi details, ensure these are setup in settings.toml |
20 | 14 | ssid = os.getenv("CIRCUITPY_WIFI_SSID") |
21 | 15 | password = os.getenv("CIRCUITPY_WIFI_PASSWORD") |
22 | 16 |
|
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" |
26 | 18 |
|
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) |
30 | 23 |
|
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}") |
34 | 35 |
|
35 | 36 | # Spilt up the cookies by ", " |
36 | 37 | elements = response.headers["set-cookie"].split(", ") |
|
49 | 50 | cookie_list.append(element) |
50 | 51 |
|
51 | 52 | # 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 | + |
56 | 56 | 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