学习【黑马程序员JavaWeb开发教程】时,课程提供的url无法访问,所以在本机使用Python语言开启一个服务。
课程地址:https://www.bilibili.com/video/BV1m84y1w7Tb?p=34
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# @Date : 2023/12/26 15:51
# @Author : HELIN
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources=r'/*') # 跨域
app.config['JSON_AS_ASCII'] = False # 返回显示中文
@app.route('/emp/list', methods=['GET'])
def get_list():
data = {
'code': 1,
'message': 'success',
'data': [
{'id': 1,
'name': '谢逊',
'image': 'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/1.jpg',
'gender': 1,
'job': '班主任',
'entrydate': '2008-05-09',
'updatedate': '2022-10-01 12:00:00'
},
{'id': 2,
'name': '殷天正',
'image': 'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/2.jpg',
'gender': 1,
'job': '讲师',
'entrydate': '2012-05-09',
'updatedate': '2022-10-01 12:00:00'
},
{'id': 3,
'name': '韦一笑',
'image': 'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/3.jpg',
'gender': 1,
'job': '讲师',
'entrydate': '2020-05-09',
'updatedate': '2022-10-01 12:00:00'
},
{'id': 4,
'name': '黛绮丝',
'image': 'https://web-framework.oss-cn-hangzhou.aliyuncs.com/web/4.jpg',
'gender': 2,
'job': '讲师',
'entrydate': '2018-05-09',
'updatedate': '2022-10-01 12:00:00'
}
]
}
return jsonify(data)
@app.route('/emp/deleteById', methods=['GET', 'POST'])
def deleteById():
data = {}
if request.method == "GET":
data = {
'errcode': 404,
'errmsg': "不存在的api,当前请求path为 /emp/deleteById, 请求方法为 GET ,请确认是否定义此请求。",
"data": 'null'
}
elif request.method == "POST":
print("params:", request.values)
data = {
'code': 1,
'message': "success",
"data": None
}
return jsonify(data)
if __name__ == '__main__':
# http://127.0.0.1:5000/emp/list
# http://127.0.0.1:5000/emp/deleteById
app.run('0.0.0.0', 5000, debug=False)