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