Python:实现一个最简单的RestAPI
需要依赖一个Python模块:【fastapi】
Documentation: https://fastapi.tiangolo.com
Source Code: https://github.com/tiangolo/fastapi
因为要使用异步网关接口(ASGI),因此需要依赖模块:【uvicorn】
首先,使用pip安装这两个模块包的依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
PS D:\adamhuan_data\pycharm_data\python_RestAPI> pip install fastapi Collecting fastapi Using cached fastapi-0.70.0-py3-none-any.whl (51 kB) Collecting starlette==0.16.0 Downloading starlette-0.16.0-py3-none-any.whl (61 kB) |████████████████████████████████| 61 kB 19 kB/s Collecting pydantic!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0,>=1.6.2 Downloading pydantic-1.8.2-cp39-cp39-win_amd64.whl (1.9 MB) |████████████████████████████████| 1.9 MB 15 kB/s Collecting anyio<4,>=3.0.0 Downloading anyio-3.3.3-py3-none-any.whl (78 kB) |████████████████████████████████| 78 kB 31 kB/s Collecting sniffio>=1.1 Downloading sniffio-1.2.0-py3-none-any.whl (10 kB) Collecting idna>=2.8 Downloading idna-3.2-py3-none-any.whl (59 kB) |████████████████████████████████| 59 kB 12 kB/s Collecting typing-extensions>=3.7.4.3 Downloading typing_extensions-3.10.0.2-py3-none-any.whl (26 kB) Installing collected packages: sniffio, idna, typing-extensions, anyio, starlette, pydantic, fastapi Successfully installed anyio-3.3.3 fastapi-0.70.0 idna-3.2 pydantic-1.8.2 sniffio-1.2.0 starlette-0.16.0 typing-extensions-3.10.0.2 WARNING: You are using pip version 21.1.2; however, version 21.3 is available. You should consider upgrading via the 'D:\adamhuan_data\pycharm_data\python_RestAPI\venv\Scripts\python.exe -m pip install --upgrade pip' command. PS D:\adamhuan_data\pycharm_data\python_RestAPI> PS D:\adamhuan_data\pycharm_data\python_RestAPI> pip install uvicorn Collecting uvicorn Downloading uvicorn-0.15.0-py3-none-any.whl (54 kB) |████████████████████████████████| 54 kB 7.8 kB/s Collecting asgiref>=3.4.0 Using cached asgiref-3.4.1-py3-none-any.whl (25 kB) Collecting click>=7.0 Downloading click-8.0.3-py3-none-any.whl (97 kB) |████████████████████████████████| 97 kB 11 kB/s Collecting h11>=0.8 Downloading h11-0.12.0-py3-none-any.whl (54 kB) |████████████████████████████████| 54 kB 22 kB/s Collecting colorama Downloading colorama-0.4.4-py2.py3-none-any.whl (16 kB) Installing collected packages: colorama, h11, click, asgiref, uvicorn Successfully installed asgiref-3.4.1 click-8.0.3 colorama-0.4.4 h11-0.12.0 uvicorn-0.15.0 WARNING: You are using pip version 21.1.2; however, version 21.3 is available. You should consider upgrading via the 'D:\adamhuan_data\pycharm_data\python_RestAPI\venv\Scripts\python.exe -m pip install --upgrade pip' command. PS D:\adamhuan_data\pycharm_data\python_RestAPI> |
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from fastapi import FastAPI import uvicorn apps1 = FastAPI() @apps1.get('/') def read_root(): return {'hello': 'world'} @apps1.get('/items/{item_id}') def read_item(item_id:int,q:str=None): return {'item_id': item_id, 'q': q} if __name__ == '__main__': uvicorn.run( app=apps1, host='0.0.0.0', port=8090 ) |
运行:
1 2 3 4 5 |
D:\adamhuan_data\pycharm_data\python_RestAPI\venv\Scripts\python.exe D:/adamhuan_data/pycharm_data/python_RestAPI/python_restapi.py INFO: Started server process [6568] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8090 (Press CTRL+C to quit) |
WEB访问:http://127.0.0.1:8090/

WEB访问:http://127.0.0.1:8090/items/5?q=you%27re%20welcome

查看帮助文档:http://127.0.0.1:8090/docs



对帮助文档进行编辑:http://127.0.0.1:8090/redoc