import flask
import json
import tools
#mock接口开发
#1、模拟接口的意思
#2、给别人提供数据
#3、flask是一个web开发框架
server = flask.Flask(__name__)#把这个python文件当做一个服务#
@server.route('/api/login',methods=['post','get'])
def login():
username = flask.request.values.get('username')#从请求里面获取到参数的
password = flask.request.values.get('password')
# flask.request.is_json#是否请求为json
# flask.request.json.get('')#入参是json的话,用这个
d = {'error_code':1,'msg':'登录成功','username':username,'password':password}
return json.dumps(d,ensure_ascii=False)
@server.route('/api/pay')
def pay():
d = {'error_code':1,'msg':'支付成功'}
return json.dumps(d,ensure_ascii=False)
@server.route('/api/get_bill')
def get_bill():
table_list = ['app_myuser','czm']
table_name = flask.request.values.get('table_name')
limit = flask.request.values.get('limit',50)
if table_name and table_name in table_list:
sql = 'select id,username,passwd from %s limit %s' % (table_name,limit)
result = tools.op_mysql(sql)
data = {'error_code':0,'msg':'成功','data':result}
else:
data = {'error_code':-1,'msg':'没有权限查询该表'}
return json.dumps(data, ensure_ascii=False)
server.run(host='0.0.0.0',port=8000,debug=True) #启动这个接口服务
python之mock接口开发(flask模块)
最新推荐文章于 2023-09-15 15:00:47 发布