跳转到主要内容

为 aiohttp.web 提供会话

项目描述

aiohttp_session

https://github.com/aio-libs/aiohttp-session/actions/workflows/ci.yaml/badge.svg?branch=master https://codecov.io/github/aio-libs/aiohttp-session/coverage.svg?branch=master https://readthedocs.org/projects/aiohttp-session/badge/?version=latest https://img.shields.io/pypi/v/aiohttp-session.svg

该库为 aiohttp.web 提供会话。

使用方法

该库允许我们将用户特定数据存储到会话对象中。

会话对象具有类似字典的接口(例如 session[key] = valuevalue = session[key] 等操作)。

在Web处理器中处理会话之前,您需要在 aiohttp.web.Application 中注册 会话中间件

一个简单的使用示例

import time
from cryptography import fernet
from aiohttp import web
from aiohttp_session import setup, get_session
from aiohttp_session.cookie_storage import EncryptedCookieStorage


async def handler(request):
    session = await get_session(request)
    last_visit = session['last_visit'] if 'last_visit' in session else None
    session['last_visit'] = time.time()
    text = 'Last visited: {}'.format(last_visit)
    return web.Response(text=text)


def make_app():
    app = web.Application()
    fernet_key = fernet.Fernet.generate_key()
    f = fernet.Fernet(fernet_key)
    setup(app, EncryptedCookieStorage(f))
    app.router.add_get('/', handler)
    return app


web.run_app(make_app())

所有存储都使用名为 AIOHTTP_SESSION 的HTTP Cookie来存储数据。这可以通过向所选存储类的构造函数传递关键字参数 cookie_name 来修改。

可用的会话存储包括

  • aiohttp_session.SimpleCookieStorage() – 将会话数据作为纯JSON字符串保存在cookie体中。仅用于测试目的,非常不安全。

  • aiohttp_session.cookie_storage.EncryptedCookieStorage(secret_key) – 将会话数据存储到cookie中,类似于 SimpleCookieStorage,但通过AES密码进行编码。 secrect_key 是AES加密/解密的 bytes 密钥,长度应为32字节。

    需要 cryptography

    $ pip install aiohttp_session[secure]
  • aiohttp_session.redis_storage.RedisStorage(redis_pool) – 将JSON编码的数据存储在 redis 中,只将redis键(一个随机UUID)保存在cookie中。 redis_pool 是一个 redis 对象,通过 await aioredis.from_url(...) 调用创建。

    $ pip install aiohttp_session[aioredis]

开发

为本地开发安装

$ make setup

运行linters

$ make lint

运行测试

$ make test

第三方扩展

许可证

aiohttp_session 在Apache 2许可下提供。

2.12.1 (2024-09-25)

  • 为aiohttp 3.10+修复了轻微的打字错误。

  • 停止支持Python 3.7。开始测试3.11 - 3.13。

2.12.0 (2022-10-28)

  • aioredis 迁移到 redis(如果使用redis而没有安装 aiohttp-session[aioredis],则必须手动安装 redis)。

2.11.0 (2021-01-31)

  • 支持直接使用 Fernet 对象初始化 EncryptedCookieStorage

  • 修复了会话在cookie过期之前被重置的问题。

2.10.0 (2021-12-30)

  • 打字支持

  • 添加samesite cookie选项

  • 支持aioredis 2

2.9.0 (2019-11-04)

  • 修复了memcached过期时间 (#398)

2.8.0 (2019-09-17)

  • 使其与Python 3.7+兼容。从collections.abc导入,而不是从collections导入。 (#373)

2.7.0 (2018-10-13)

  • 如果会话年龄 > max_age,则重置会话 (#331)

  • 对于EncryptedCookieStorage,在TTL过期时重置会话 (#326)

2.6.0 (2018-09-12)

  • 如果 NaClCookieStorage 无法解码损坏的cookie,则创建一个新的会话 (#317)

2.5.0 (2018-05-12)

  • 添加请求新会话的API (#281)

2.4.0 (2018-05-04)

  • 修复了会话固定的问题 (#272)

2.3.0 (2018-02-13)

  • 所有存储支持自定义编码器和解码器 (#252)

  • 升级到aiohttp 3.0

2.2.0 (2018-01-31)

  • 修复了错误处理中坏中间件返回类型的格式问题。 (#249)

2.1.0 (2017-11-24)

  • 添加 session.set_new_identity() 方法以更改新会话的标识 (#236)

2.0.1 (2017-11-22)

  • 将aioredis安装检查中的断言替换为 RuntimeError (#235)

2.0.0 (2017-11-21)

  • 更新到aioredis 1.0+。aiohttp-session 2.0 与aioredis 0.X 不兼容 (#234)

1.2.1 (2017-11-20)

  • 固定aioredis <1.0 (#231)

1.2.0 (2017-11-06)

  • 添加MemcachedStorage (#224)

1.1.0 (2017-11-03)

  • 将中间件升级到aiohttp 2.3+的新样式

1.0.1 (2017-09-13)

  • 为redis_storage添加key_factory属性 (#205)

1.0.0 (2017-07-27)

  • 在RedisStorage数据加载时捕获解码器异常 (#175)

  • 在删除cookie时指定域和路径 (#171)

0.8.0 (2016-12-04)

  • 使用 time.time() 而不是 time.monotonic() 作为绝对时间 (#81)

0.7.0 (2016-09-24)

  • 修复测试以与aiohttp上游API兼容,以支持客户端cookies

0.6.0 (2016-09-08)

  • 自动添加expires字段以支持较旧的浏览器 (#43)

  • 在redis存储中尊重session.max_age (#45)

  • 始终将存储中的默认max_age传递给会话 (#45)

0.5.0 (2016-02-21)

  • 通过提供空会话处理cryptography.fernet.InvalidToken异常 (#29)

0.4.0 (2016-01-06)

  • 添加可选的NaCl加密存储 (#20)

  • 放宽EncryptedCookieStorage以接受由Fernet.generate_key生成的base64编码的字符串。

  • 添加setup()函数

  • 即使在中间件链中出现异常,也会保存会话

0.3.0 (2015-11-20)

  • 反映 aiohttp 变更:最低要求的 Python 版本是 3.4.1

  • 使用显式的 'aiohttp_session' 包

0.2.0 (2015-09-07)

  • 添加 session.created 属性 (#14)

  • 用 crypthography 库替换 PyCrypto (#16)

0.1.2 (2015-08-07)

  • 添加清单文件 (#15)

0.1.1 (2015-04-20)

  • 修复 #7:停止每次保存会话时 cookie 名称增长

0.1.0 (2015-04-13)

  • 首次公开发布

项目详情


下载文件

下载适合您平台的文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。

源代码分发

aiohttp_session-2.12.1.tar.gz (92.8 kB 查看哈希值)

上传时间 源代码

构建分发

aiohttp_session-2.12.1-py3-none-any.whl (12.5 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面