|
796 | 796 | " if arg.lower()=='htmx': return _get_htmx(hdrs)\n", |
797 | 797 | " if arg.lower()=='app': return ws.scope['app']\n", |
798 | 798 | " if arg.lower()=='send': return partial(_send_ws, ws)\n", |
| 799 | + " if 'session'.startswith(arg.lower()): return ws.scope.get('session', {})\n", |
799 | 800 | " return None\n", |
800 | 801 | " res = data.get(arg, None)\n", |
801 | 802 | " if res is empty or res is None: res = hdrs.get(arg, None)\n", |
|
1228 | 1229 | { |
1229 | 1230 | "data": { |
1230 | 1231 | "text/plain": [ |
1231 | | - "'a604e4a2-08e8-462d-aff9-15468891fe09'" |
| 1232 | + "'77486da1-c613-48be-80c4-9cae89eeec48'" |
1232 | 1233 | ] |
1233 | 1234 | }, |
1234 | 1235 | "execution_count": null, |
|
1748 | 1749 | "name": "stdout", |
1749 | 1750 | "output_type": "stream", |
1750 | 1751 | "text": [ |
1751 | | - "Message text was: Hi!, from client: Address(host='testclient', port=50000)\n" |
| 1752 | + "Message text was: Hi! with session bar, from client: Address(host='testclient', port=50000)\n" |
1752 | 1753 | ] |
1753 | 1754 | } |
1754 | 1755 | ], |
1755 | 1756 | "source": [ |
| 1757 | + "@app.get(\"/setsess\")\n", |
| 1758 | + "def set_sess(session):\n", |
| 1759 | + " session['foo'] = 'bar'\n", |
| 1760 | + " return 'ok'\n", |
| 1761 | + "\n", |
1756 | 1762 | "@app.ws(\"/ws\")\n", |
1757 | | - "def ws(self, msg:str, ws:WebSocket): return f\"Message text was: {msg}, from client: {ws.client}\"\n", |
| 1763 | + "def ws(self, msg:str, ws:WebSocket, session): return f\"Message text was: {msg} with session {session.get('foo')}, from client: {ws.client}\"\n", |
| 1764 | + "\n", |
| 1765 | + "cli.get('/setsess')\n", |
1758 | 1766 | "with cli.websocket_connect('/ws') as ws:\n", |
1759 | 1767 | " ws.send_text('{\"msg\":\"Hi!\"}')\n", |
1760 | 1768 | " data = ws.receive_text()\n", |
1761 | | - "assert 'Message text was: Hi!' in data\n", |
| 1769 | + "assert 'Message text was: Hi! with session bar' in data\n", |
1762 | 1770 | "print(data)" |
1763 | 1771 | ] |
1764 | 1772 | }, |
|
2415 | 2423 | "name": "stdout", |
2416 | 2424 | "output_type": "stream", |
2417 | 2425 | "text": [ |
2418 | | - "Set to 2024-10-28 20:22:34.772989\n" |
| 2426 | + "Set to 2024-11-04 15:30:23.038930\n" |
2419 | 2427 | ] |
2420 | 2428 | }, |
2421 | 2429 | { |
2422 | 2430 | "data": { |
2423 | 2431 | "text/plain": [ |
2424 | | - "'Session time: 2024-10-28 20:22:34.772989'" |
| 2432 | + "'Session time: 2024-11-04 15:30:23.038930'" |
2425 | 2433 | ] |
2426 | 2434 | }, |
2427 | 2435 | "execution_count": null, |
|
2786 | 2794 | { |
2787 | 2795 | "data": { |
2788 | 2796 | "text/plain": [ |
2789 | | - "'Cookie was set at time 20:22:35.467691'" |
| 2797 | + "'Cookie was set at time 15:30:23.174646'" |
2790 | 2798 | ] |
2791 | 2799 | }, |
2792 | 2800 | "execution_count": null, |
|
0 commit comments