Build web services in 1 minute with Python

Python installation reference: centOs7 installs Python3

pip3 install fastapi uvicorn -i https://pypi.tuna.tsinghua.edu.cn/simple && \
ln -s /usr/local/python3/bin/uvicorn /usr/bin/uvicorn

main.py

from fastapi import FastAPI, Query

app = FastAPI()

@app.get("/")
def hello():
    return {
    
    "Hello": "World"}

@app.post('/user/update')
async def update_user(
    *,
    user_id: int,
    really_update: int = Query(...)
): return {
    
    "status": "SUCCESS"}

@app.get('/user')
async def user(
        *,
        user_id: int = Query(..., title="The ID", gt=0)
): return {
    
    'user_id': user_id}

start service

uvicorn main:app --host 0.0.0.0 --port 8080 --reload
  • --host 0.0.0.0: allow all ip access
  • --port 8080: listening port

url:http://127.0.0.1:8080
docs:http://127.0.0.1:8080/docs

Guess you like

Origin blog.csdn.net/wkh___/article/details/130129480