python基础篇 27-flask基本的mock
flask基本mock
import flask # 轻量级web开发框架 from flask import jsonify import tools server = flask.Flask(__name__) @server.route('/login',methods=['post','get']) def login(): username = flask.request.values.get('username') password = flask.request.values.get('password') # # 如果是json请求 则 # flask.json.get('xxx') # # 获取cookie中的数据 # flask.request.cookies.get('xxxx') # # 获取headers中的信息 # flask.request.headers.get('xxx') # 获取文件 # flask.request.files.get('xxx') username = username.strip() password = password.strip() if username and password: sql = f"select * from subscriber where username = '{username}'and is_delete=0;" if tools.execute_sql(sql): sql_p = f"select password from subscriber where username ='{username}' and is_delete=0;" pwd = tools.execute_sql(sql_p)[0].get('password') p = tools.my_md5(password) if p == pwd: return jsonify({'code': 0, 'msg': '用户%s登陆成功!' %username} ) else: return jsonify({'code': -1, 'msg': '密码不对!'}) else: return jsonify({'code': -1, 'msg': '用户%s不存在!' % username}) else: return jsonify({'code': -1, 'msg': '必填参数不能为空!'}) @server.route('/reg',methods=['post','get']) def reg(): username = flask.request.values.get('username').strip() password = flask.request.values.get('password').strip() cpassword = flask.request.values.get('cpassword').strip() if username and password and cpassword: if password != cpassword: return {'code':-1,'msg':'两次输出的密码不一致!'} else: query_sql = f"select * from subscriber where username ='{username}' and is_delete=0;" if tools.execute_sql(query_sql): return {'code':-1,'msg':'用户%s已经存在' %username} else: p = tools.my_md5(password) insert_sql = f"insert into subscriber(username,password) values ('{username}','{p}');" tools.execute_sql(insert_sql) if tools.execute_sql(query_sql): return {'code':0,'msg':'注册用户%s成功' %username} else: return {'code':-1,'msg':'必填参数不能为空'} server.config['JSON_AS_ASCII'] = False #配置支持返回中文 server.run(host='0.0.0.0',port=8999,debug=True)