Der Raspberry Pi Pico W ist pure Magie. Ein Microcontroller, in Python zu steuern, mit WLAN on board 😍 Super einfach, damit irgendwas im Netz zu machen – essentiell für IoT, looking at you, Arduino.
(Aber man muss|Außerdem kann man) viel über MicroPython lernen. Zum Beispiel, wie urequests Response-Header parst: Als Key/Value. Das ist uncool, da es durchaus Header gibt, die doppelt kommen können und sollen:
The optional
replaceparameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass infalseas the second argument you can force multiple headers of the same type. For example:
Sagt PHP (replace Parameter).
The
Set-CookieHTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multipleSet-Cookieheaders should be sent in the same response.
Sagt Mozilla.
Imho nicht unbedingt unseriöse Quellen, aber für MicroPython ist das trotzdem kein schlagendes Argument; Header duplicates werden weiterhin nicht unterstützt werden. Siehe meine längliche Diskussion ab hier.
Es gibt aber einen Workaround, und der ist imho nicht offensichtlich: Man kann nämlich urequests.post() mit einem Parameter parse_headers aufrufen, der erstmal ein Boolean ist – aber auch eine Callback-Funktion sein kann:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import urequests import ujson cookie_jar = [] # ... def cookie_monster(header, ignore): global cookie_jar if header.startswith(b"Set-Cookie:"): header = str(header, "utf-8") key, cookie = header.split(":", 1) cookie += ';' # in case there isn't a ; yet cookie, suffix = cookie.split(";", 1) cookie_jar.append(cookie.strip()) # ... json = ujson.dumps({ 'foor': BAR, 'x': Y }) request = urequests.post('https://example.com/foobar', headers = HEADERS, data = json, parse_headers = cookie_monster) request.close() print('; '.join(cookie_jar)) |
Das Fragment (ich hoffe, ich habe auf die Schnelle nichts vergessen) printed alle Cookies. HTH.