Python fastapi的简单使用

brief

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

The key features are:
  • Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.

  • Fast to code: Increase the speed to develop features by about 200% to 300%. *

  • Fewer bugs: Reduce about 40% of human (developer) induced errors. *

  • Intuitive: Great editor support. Completion everywhere. Less time debugging.

  • Easy: Designed to be easy to use and learn. Less time reading docs.

  • Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.

  • Robust: Get production-ready code. With automatic interactive documentation.

  • Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.

setup

  1. install fastapi
pip install fastapi
  1. install ASGI server
pip install uvicorn

demo

//main.py

import datetime
import fastapi

app = fastapi.FastAPI()


@app.get("/now")
async def get_time():
   time = datetime.datetime.now()
   return time.strftime("%Y-%m-%d %H:%M:%S")

run

uvicorn fastapi_demo:app --reload

as flow may be successful:
在这里插入图片描述

test

http://127.0.0.1:8000/now

the result maybe:
在这里插入图片描述

details info

reference

猜你喜欢

转载自blog.csdn.net/lianshaohua/article/details/113398919