aio sanic sse
Project description
sanic_sse ########
- 安装 ==========
.. code-block:: shell
pip install sanic-sse-py3
- 示例 ==========
- 2.1 代码
.. code-block:: python
import os
import sys
from sanic import Sanic
from sanic.response import html
sys.path.insert(0, os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
from sse.api.urls import sse_bgp
from sse import SseApp
app = Sanic(name="sse")
SSE_CONFIG = {
"pubsub_options": {
"redis_host": os.getenv("SSE_REDIS_HOST", "127.0.0.1"),
"redis_port": int(os.getenv("SSE_REDIS_PORT", "16379")),
"redis_passwd": os.getenv("SSE_REDIS_PASSWD", ""),
},
"ping_interval": int(os.getenv("SSE_PING_INTERVAL", 10)),
}
def init_app(_app):
_app.ctx.sse_config = SSE_CONFIG
SseApp(_app)
_app.blueprint(sse_bgp)
@app.route("/index", methods=["GET"])
async def index(request):
event = request.args.get("event", "test")
url = f"sse/event/listen?event={event}"
d = """
<html>
<body>
<script>
var source = new EventSource("%s");
source.onmessage = function(e) {
console.log("xxxxxxx");
document.getElementById('response').innerHTML + e.data + "<br>";
}
</script>
<h1>Getting server updates</h1>
<div id="response"></div>
</body>
</html>
""" % url
return html(body=d)
if __name__ == "__main__":
init_app(app)
app.run(host="0.0.0.0", port=8008, workers=10)
- 2.2 接口
.. code-block:: shell
GET /sse/event/send?event=test
GET /sse/event/listen?event=test&client_id=
GET /sse/event/terminate?event=test&client_id=
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sanic-sse-py3-1.0.8.tar.gz
(9.2 kB
view details)
Built Distribution
File details
Details for the file sanic-sse-py3-1.0.8.tar.gz
.
File metadata
- Download URL: sanic-sse-py3-1.0.8.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9df8b40344f9cbffd1b8c84f0f80d5972f631d98b457ceee065ce19423c726d2 |
|
MD5 | 7f31da48f932b55ea6a8288ccee8ee48 |
|
BLAKE2b-256 | acc023f03dd49b6b3a1b0d7a6e186faddcedd222073efead7fb78ca4df1e6bf6 |
File details
Details for the file sanic_sse_py3-1.0.8-py3-none-any.whl
.
File metadata
- Download URL: sanic_sse_py3-1.0.8-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e143463a78f02510196499798f9bceb4c88c5fa439813fe7afd5a62792449284 |
|
MD5 | 21e39de7659512cf012dcaeaceef3ddc |
|
BLAKE2b-256 | fedf3f8a8bf2122bffb2467933613b75bf7b00786f0e8e276bffd7fd52808db7 |