sanic/examples/run_asgi.py

48 lines
853 B
Python
Raw Normal View History

2019-05-21 23:42:19 +01:00
"""
1. Create a simple Sanic app
2. Run with an ASGI server:
$ uvicorn run_asgi:app
or
$ hypercorn run_asgi:app
"""
2019-05-26 22:57:50 +01:00
import os
from sanic import Sanic, response
2019-05-21 23:42:19 +01:00
app = Sanic(__name__)
2019-05-26 22:57:50 +01:00
@app.route("/text")
2019-05-21 23:42:19 +01:00
def handler(request):
2019-05-26 22:57:50 +01:00
return response.text("Hello")
2019-05-21 23:42:19 +01:00
2019-05-26 22:57:50 +01:00
@app.route("/json")
2019-05-21 23:42:19 +01:00
def handler_foo(request):
2019-05-26 22:57:50 +01:00
return response.text("bar")
2019-05-21 23:42:19 +01:00
2019-05-26 22:57:50 +01:00
@app.websocket("/ws")
2019-05-21 23:42:19 +01:00
async def feed(request, ws):
name = "<someone>"
while True:
data = f"Hello {name}"
await ws.send(data)
name = await ws.recv()
if not name:
break
2019-05-26 22:57:50 +01:00
@app.route("/file")
async def test_file(request):
return await response.file(os.path.abspath("setup.py"))
@app.route("/file_stream")
async def test_file_stream(request):
return await response.file_stream(
os.path.abspath("setup.py"), chunk_size=1024
)