web框架介绍 flask使用介绍 fastapi使用介绍 websocket使用介绍
- 2025-11-05 00:01:00
- admin
- 原创 414
一、web框架介绍
1、python2使用mysqldb访问数据库;
2、python3使用pymysql访问数据库;
3、ORM框架有Django和SQLAlchemy;
4、WEB框架排名:Flask > Django > FastAPI
5、Flask是比较轻量的框架,FastAPI是高性能的异步编程框架;
二、flask使用介绍
1、代码仓库:https://github.com/pallets/flask
2、帮助文档:https://flask.palletsprojects.com/en/stable
3、接口文档:https://flask.palletsprojects.com/en/stable/api
4、快速开始:https://flask.palletsprojects.com/en/stable/quickstart
5、安装flask:pip install flask
flask常用函数:
1、url_for:https://flask.palletsprojects.com/en/stable/api/#flask.url_for
2、url_for:https://flask.palletsprojects.com/en/stable/quickstart/#variable-rules
3、jsonify:https://flask.palletsprojects.com/en/stable/api/#flask.json.jsonify
flask常用函数:
1、url_for:根据视图函数以及给定的值生成一个动态URL,视图函数是一个接口;
2、jsonify:组装一个json或list返回,对象本身是json或list可以直接返回;
三、fastapi使用介绍
1、框架的代码仓库:https://github.com/fastapi/fastapi
2、服务的代码仓库:https://github.com/Kludex/uvicorn
3、帮助文档:https://fastapi.tiangolo.com/#installation
4、安装fastapi:pip install "fastapi[standard]"
四、websocket使用介绍
1、协议规范:https://datatracker.ietf.org/doc/html/rfc6455
2、浏览器API:https://developer.mozilla.org/zh-CN/docs/Web/API/WebSockets_API
3、协议内容:握手协议 - 用于将HTTP切换为WebSocket,数据传输 - 用于后续数据传输;
4、frame:协议层的最小传输单元是帧,一个或多个帧组成一个消息,目前一共有六种帧;
5、frame:0x0延续帧,0x1文本帧,0x2二进制帧,0x8关闭帧,0x9探测帧,0xA应答帧;
1、代码仓库:https://github.com/python-websockets/websockets
2、帮助文档:https://websockets.readthedocs.io/en/stable
3、IO模型:支持多种IO模型,同时支持asyncio、threading、trio
4、IO模型:服务端推荐用asyncio,客户端推荐用threading
5、安装依赖:pip install websockets
websocket-client(不推荐)
1、代码仓库:https://github.com/websocket-client/websocket-client
2、帮助文档:https://websocket-client.readthedocs.io/en/latest
3、安装依赖:pip install websocket-client