项目作者: abersheeran

项目描述 :
Strictly compliant WSGI & ASGI framework/toolkit.
高级语言: Python
项目地址: git://github.com/abersheeran/baize.git
创建时间: 2021-02-21T04:51:11Z
项目社区:https://github.com/abersheeran/baize

开源协议:Apache License 2.0

下载


BáiZé

Codecov
PyPI - Python Version

Powerful and exquisite WSGI/ASGI framework/toolkit. Only relies on the standard library.

The minimize implementation of methods required in the Web framework. No redundant implementation means that you can freely customize functions without considering the conflict with baize’s own implementation.

Under the ASGI/WSGI protocol, the interface of the request object and the response object is almost the same, only need to add or delete await in the appropriate place. In addition, it should be noted that ASGI supports WebSocket but WSGI does not.

Install

  1. pip install -U baize

Document and other website

BáiZé Document

If you have questions or idea, you can send it to Discussions.

Quick Start

A short example for WSGI application, if you don’t know what is WSGI, please read PEP3333.

  1. import time
  2. from typing import Callable
  3. from baize.wsgi import (
  4. decorator,
  5. request_response,
  6. Router,
  7. Request,
  8. Response,
  9. PlainTextResponse,
  10. )
  11. @decorator
  12. def timer(request: Request, next_call: Callable[[Request], Response]) -> Response:
  13. start_time = time.time()
  14. response = next_call(request)
  15. end_time = time.time()
  16. response.headers["x-time"] = str(round((end_time - start_time) * 1000))
  17. return response
  18. @request_response
  19. @timer
  20. def sayhi(request: Request) -> Response:
  21. return PlainTextResponse("hi, " + request.path_params["name"])
  22. @request_response
  23. @timer
  24. def echo(request: Request) -> Response:
  25. return PlainTextResponse(request.body)
  26. application = Router(
  27. ("/", PlainTextResponse("homepage")),
  28. ("/echo", echo),
  29. ("/sayhi/{name}", sayhi),
  30. )
  31. if __name__ == "__main__":
  32. import uvicorn
  33. uvicorn.run(application, interface="wsgi", port=8000)

A short example for ASGI application, if you don’t know what is ASGI, please read ASGI Documention.

  1. import time
  2. from typing import Awaitable, Callable
  3. from baize.asgi import (
  4. decorator,
  5. request_response,
  6. Router,
  7. Request,
  8. Response,
  9. PlainTextResponse,
  10. )
  11. @decorator
  12. async def timer(
  13. request: Request, next_call: Callable[[Request], Awaitable[Response]]
  14. ) -> Response:
  15. start_time = time.time()
  16. response = await next_call(request)
  17. end_time = time.time()
  18. response.headers["x-time"] = str(round((end_time - start_time) * 1000))
  19. return response
  20. @request_response
  21. @timer
  22. async def sayhi(request: Request) -> Response:
  23. return PlainTextResponse("hi, " + request.path_params["name"])
  24. @request_response
  25. @timer
  26. async def echo(request: Request) -> Response:
  27. return PlainTextResponse(await request.body)
  28. application = Router(
  29. ("/", PlainTextResponse("homepage")),
  30. ("/echo", echo),
  31. ("/sayhi/{name}", sayhi),
  32. )
  33. if __name__ == "__main__":
  34. import uvicorn
  35. uvicorn.run(application, interface="asgi3", port=8000)

License

Apache-2.0.