跳转到主要内容

aiohttp的服务端事件支持。

项目描述

https://github.com/aio-libs/aiohttp-sse/workflows/CI/badge.svg?event=push https://codecov.io/gh/aio-libs/aiohttp-sse/branch/master/graph/badge.svg Updates Chat on Gitter

EventSource 接口用于接收服务端发送的事件。它通过HTTP连接到服务器,并接收文本/event-stream格式的数据,而不会关闭连接。 aiohttp-sseaiohttp 提供了服务端事件支持。

安装

安装过程非常简单

$ pip install aiohttp-sse

示例

import asyncio
from datetime import datetime

from aiohttp import web

from aiohttp_sse import sse_response


async def hello(request: web.Request) -> web.StreamResponse:
    async with sse_response(request) as resp:
        while resp.is_connected():
            time_dict = {"time": f"Server Time : {datetime.now()}"}
            data = json.dumps(time_dict, indent=2)
            print(data)
            await resp.send(data)
            await asyncio.sleep(1)
    return resp


async def index(_request: web.Request) -> web.StreamResponse:
    html = """
        <html>
            <body>
                <script>
                    var eventSource = new EventSource("/hello");
                    eventSource.addEventListener("message", event => {
                        document.getElementById("response").innerText = event.data;
                    });
                </script>
                <h1>Response from server:</h1>
                <div id="response"></div>
            </body>
        </html>
    """
    return web.Response(text=html, content_type="text/html")


app = web.Application()
app.router.add_route("GET", "/hello", hello)
app.router.add_route("GET", "/", index)
web.run_app(app, host="127.0.0.1", port=8080)

EventSource协议

要求

许可证

aiohttp-sse 在Apache 2.0许可证下提供。

项目详情


下载文件

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

源代码分发

aiohttp-sse-2.2.0.tar.gz (9.4 kB 查看哈希值)

上传时间 源代码

构建分发

aiohttp_sse-2.2.0-py3-none-any.whl (6.7 kB 查看哈希值)

上传时间 Python 3

支持者

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