socketify.py/bench/asgi_wsgi/falcon-asgi.py

27 wiersze
877 B
Python
Czysty Zwykły widok Historia

2022-12-01 00:42:07 +00:00
import falcon.asgi
from socketify import ASGI
class Home:
async def on_get(self, req, resp):
resp.status = falcon.HTTP_200 # This is the default status
resp.content_type = falcon.MEDIA_TEXT # Default is JSON, so override
resp.text = "Hello, World!"
async def on_post(self, req, resp):
2023-01-06 19:11:19 +00:00
# curl -d '{"name":"test"}' -H "Content-Type: application/json" -X POST http://localhost:8000/
json = await req.media
2022-12-01 00:42:07 +00:00
resp.status = falcon.HTTP_200 # This is the default status
resp.content_type = falcon.MEDIA_TEXT # Default is JSON, so override
2023-01-06 19:11:19 +00:00
resp.text = json.get("name", "")
2022-12-01 00:42:07 +00:00
app = falcon.asgi.App()
home = Home()
app.add_route("/", home)
if __name__ == "__main__":
2022-12-07 12:38:42 +00:00
ASGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(workers=8)