16.FastAPI session

16.FastAPI session

在FastAPI中,要使用session,实际上是使用Starlette Session,Starlette Session是作为一个中间件存在的,所以需要安装Starlette Session。执行命令:

pip install starlette-session 

16.1使用Redis作为session存储

代码如下:

from fastapi import FastAPI
from redis import Redis
from starlette_session import SessionMiddleware
from starlette_session.backends import BackendType
​
app = FastAPI()
​
redis_client = Redis(host="127.0.0.1", port=6379, password="xxxxxx")
​
app.add_middleware(
    SessionMiddleware,
    secret_key="secret",
    cookie_name="cookie22",
    backend_type=BackendType.redis,
    backend_client=redis_client
)

16.2使用Redis Cluster作为session存储

代码如下:

from fastapi import FastAPI
from rediscluster import RedisCluster
from starlette_session import SessionMiddleware
from starlette_session.backends import BackendType
​
app = FastAPI()
​
redis_client = RedisCluster(host="127.0.0.1", port=6381, password="xxxxxx")
​
app.add_middleware(
    SessionMiddleware,
    secret_key="secret",
    cookie_name="cookie22",
    backend_type=BackendType.redis,
    backend_client=redis_client
)

16.3操作session

在FastAPI中,操作session可以通过request对象直接进行操作,假设对象名为request,则:

  1. request.session.update()

    设置session,参数为字典

  2. request.session.clear()

    清除session

  3. request.session

    读取session

代码如下:

from fastapi import FastAPI
from fastapi import Request
from rediscluster import RedisCluster
from starlette_session import SessionMiddleware
from starlette_session.backends import BackendType
​
app = FastAPI()
​
redis_client = RedisCluster(host="127.0.0.1", port=6381, password="jane10181010")
​
app.add_middleware(
    SessionMiddleware,
    secret_key="secret",
    cookie_name="cookie22",
    backend_type=BackendType.redis,
    backend_client=redis_client
)
​
@app.get(path='/set')
async def set(request: Request):
    request.session.update({"uid": "10181010"})
    return {'res': True}
​
@app.get(path='/cls')
async def cls(request: Request):
    request.session.clear()
    return {'res': True}
​
@app.get(path='/get')
async def get(request: Request):
    return {'session': request.session}

执行请求:

curl http://127.0.0.1:8000/set -i
HTTP/1.1 200 OK
date: Tue, 18 Jan 2022 02:20:00 GMT
server: uvicorn
content-length: 12
content-type: application/json
set-cookie: cookie22=eyJfY3NzaWQiOiAiOGExMDJiM2UtZjJiNS00ZmVmLWJhZWEtYWM5YjI4ZWQ1N2JlIn0=.YeYj0Q.UCFZKi3jalMw0SknLsmM_RYpSj0; Path=/; Max-Age=1209600; httponly; samesite=lax
​
{"res":true}
​
curl http://127.0.0.1:8000/get --cookie "cookie22=eyJfY3NzaWQiOiAiOGExMDJiM2UtZjJiNS00ZmVmLWJhZWEtYWM5YjI4ZWQ1N2JlIn0=.YeYj0Q.UCFZKi3jalMw0SknLsmM_RYpSj0; Path=/; Max-Age=1209600; httponly; samesite=lax" -i
HTTP/1.1 200 OK
date: Tue, 18 Jan 2022 02:20:26 GMT
server: uvicorn
content-length: 30
content-type: application/json
set-cookie: cookie22=eyJfY3NzaWQiOiAiOGExMDJiM2UtZjJiNS00ZmVmLWJhZWEtYWM5YjI4ZWQ1N2JlIn0=.YeYj6w.H2gL6V-BTq4ePB9gaTg-3fAt5r0; Path=/; Max-Age=1209600; httponly; samesite=lax
​
{"session":{"uid":"10181010"}}
​
curl http://127.0.0.1:8000/cls --cookie "cookie22=eyJfY3NzaWQiOiAiOGExMDJiM2UtZjJiNS00ZmVmLWJhZWEtYWM5YjI4ZWQ1N2JlIn0=.YeYj0Q.UCFZKi3jalMw0SknLsmM_RYpSj0; Path=/; Max-Age=1209600; httponly; samesite=lax" -i
HTTP/1.1 200 OK
date: Tue, 18 Jan 2022 02:20:48 GMT
server: uvicorn
content-length: 12
content-type: application/json
set-cookie: cookie22=null; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; httponly; samesite=lax
​
{"res":true}
​
curl http://127.0.0.1:8000/get --cookie "cookie22=eyJfY3NzaWQiOiAiOGExMDJiM2UtZjJiNS00ZmVmLWJhZWEtYWM5YjI4ZWQ1N2JlIn0=.YeYj0Q.UCFZKi3jalMw0SknLsmM_RYpSj0; Path=/; Max-Age=1209600; httponly; samesite=lax" -i
HTTP/1.1 200 OK
date: Tue, 18 Jan 2022 02:20:53 GMT
server: uvicorn
content-length: 16
content-type: application/json
set-cookie: cookie22=null; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; httponly; samesite=lax
​
{"session":null}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Janeb1018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值