bonfy / gitblog

Long Live Git Blog
3 stars 0 forks source link

初尝FastAPI #5

Open bonfy opened 3 years ago

bonfy commented 3 years ago

FastAPI 可谓说目前Python Web Framework 中的当红炸子鸡,一直只闻其名,没有尝试过,今天抽空也尝试一把。

主页

安装

$ pip3 install fastapi
$ pip3 install uvicorn # run as server

$ uvicorn main:app --reload

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [28720]
INFO:     Started server process [28722]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

参考资料

bonfy commented 3 years ago

main.py

# coding: utf-8

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return {'status': 'ok'}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
$ uvicorn main:app --reload --host 0.0.0.0 --port 8000

Gunicorn

$ pip3 install  gunicorn

$ gunicorn -w 4 -b 0.0.0.0:5000  manage:app -D