ppt
flask 的参考文档
Flask的安装
pip install flask
http的方法
HTTP请求方法大全 - 常用参考表对照表 - 脚本之家在线工具 (jb51.net)
常用的方法有 get、post、put、delete
请求参数的处理
# 导入 Flask 的模块
from flask import Flask, request
# 创建一个 Flask 应用的程序实例
app = Flask(__name__)
# get 请求数据
@app.route("/courses", methods=["get"])
def get_course():
name = request.args.get("name")
print(name)
return {"code": 0, "msg": f"get {name} success"}
# json 参数
@app.route("/add_courses", methods=["post"])
def add_course():
print(request.json)
return {"code": 0, "msg": "post success"}
# 表单请求
@app.route("/login", methods=["post"])
def user_login():
name = request.form.get("name")
psw = request.form.get("password")
print(f"收到前端的登录参数 name 为 {name}, password 为 {psw}")
if name == "root" and psw == "123456":
return {"code": 0, "msg": "login success"}
else:
return {"code": 401, "msg": "login fail"}
user_list = [
{"username": "zhangsan", "password": "123456"},
{"username": "lisi", "password": "12345"},
{"username": "wangwu", "password": "12346"},
{"username": "zhaoliu", "password": "12456"},
{"username": "niuqi", "password": "123476"},
]
@app.route("/new_login", methods=["post"])
def user_login_new():
username = request.form.get("username")
psw = request.form.get("password")
print(f"收到前端的登录参数 username 为 {username}, password 为 {psw}")
for user_obj in user_list:
print(f'当前查询的用户是{user_obj.get("username")},密码是{user_obj.get("username")}')
if username == user_obj.get("username") and psw == user_obj.get("password"):
return {"code": 0, "msg": "success", "data": {"name": username, "password": psw}}
return {"code": 401, "msg": "login fail"}
# 处理文件请求
@app.route("/file_req", methods=["post"])
def file_req():
file_obj = request.files.get("file")
print(file_obj)
if file_obj is None:
# 表示没有发送文件
return {"code": 40001, "msg": "file not upload"}
file_obj.save("test_file.png")
return {"code": 0, "msg": "file upload success"}
使用蓝图注册
server.py
# # 导入 Flask 的模块
from flask import Flask
from user_route import user_bp
# 创建一个 Flask 应用的程序实例
app = Flask(__name__)
# 注册蓝图
app.register_blueprint(user_bp)
# 启动app程序
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)
user_route.py
from flask import Blueprint
user_bp = Blueprint("用户管理", __name__, url_prefix="/user")
@user_bp.route("/")
def user():
return "用户列表"
@user_bp.route("/login")
def user_login():
return "登录成功"
@user_bp.route("/register")
def user_register():
return "注册成功"
@user_bp.route("/delete")
def delete_user():
return "删除成功"
restful 风格的接口代码
from flask import Flask, request
from data.data import courses
app = Flask(__name__)
@app.route("/courses", methods=["get"])
def get_course():
# 获取课程
course_id = request.args.get("course_id")
print(course_id)
datas = []
if course_id:
# 如果不为空,则返回对应的课程
for course in courses:
if int(course_id) == course.get("course_id"):
datas.append(course)
else:
# 返回所有数据
datas = courses
return {"code": 0, "msg": "success", "data": datas}
@app.route("/courses", methods=["post"])
def post_course():
# 添加课程
course_data = request.json
print(f"接收到的参数<====== {course_data}")
course_id = int(course_data.get("course_id"))
course_name = course_data.get("course_name")
course_desc = course_data.get("course_desc")
new_course = {
"course_id": course_id,
"course_name": course_name,
"course_desc": course_desc,
}
# 判断是否有课程
for course in courses:
if course.get("course_id") == course_id:
return {"code": 40001, "msg": "course id is exists"}
courses.append(new_course)
print(f"当前所有课程:{courses}")
return {"code": 0, "msg": "success", "data": new_course}
@app.route("/courses", methods=["put"])
def put_course():
# 修改课程
course_data = request.json
print(f"接收到的参数<====== {course_data}")
course_id = int(course_data.get("course_id"))
course_name = course_data.get("course_name")
course_desc = course_data.get("course_desc")
update_course = {
"course_id": course_id,
"course_name": course_name,
"course_desc": course_desc,
}
for course in courses:
if course.get("course_id") == course_id:
# 先删除,再添加
print(f"需要更新的课程:{course}")
courses.remove(course)
courses.append(update_course)
print(f"当前所有课程:{courses}")
return {"code": 0, "msg": "success", "data": update_course}
return {"code": 40001, "msg": "course is not exists"}
@app.route("/courses", methods=["delete"])
def del_course():
# 删除课程
course_id = int(request.json.get("course_id"))
for course in courses:
if course.get("course_id") == course_id:
print(f"删除课程:{course}")
courses.remove(course)
print(f"当前所有课程:{courses}")
return {"code": 0, "msg": "success"}
return {"code": 40001, "msg": "course is not exists"}
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5008, debug=True)