forked from adafruit/Adafruit_CircuitPython_Requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests_multiple_cookies.py
58 lines (48 loc) · 1.73 KB
/
requests_multiple_cookies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# SPDX-FileCopyrightText: 2022 Alec Delaney
# SPDX-License-Identifier: MIT
"""
This example was written for the MagTag; changes may be needed
for connecting to the internet depending on your device.
"""
import ssl
import wifi
import socketpool
import adafruit_requests
COOKIE_TEST_URL = "https://www.adafruit.com"
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# Connect to the Wi-Fi network
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
# Set up the requests library
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# GET from the URL
print("Fetching multiple cookies from", COOKIE_TEST_URL)
response = requests.get(COOKIE_TEST_URL)
# Spilt up the cookies by ", "
elements = response.headers["set-cookie"].split(", ")
# NOTE: Some cookies use ", " when describing dates. This code will iterate through
# the previously split up 'set-cookie' header value and piece back together cookies
# that were accidentally split up for this reason
days_of_week = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
elements_iter = iter(elements)
cookie_list = []
for element in elements_iter:
captured_day = [day for day in days_of_week if element.endswith(day)]
if captured_day:
cookie_list.append(element + ", " + next(elements_iter))
else:
cookie_list.append(element)
# Pring the information about the cookies
print("Number of cookies:", len(cookie_list))
print("")
print("Cookies received:")
print("-" * 40)
for cookie in cookie_list:
print(cookie)
print("-" * 40)