import Flask
import json
from Flask import request
server1 = flask.Flask(__name__) #实例化一个flask对象
@server1.route('/login',methods=['get','post']) #@server.route()可以将普通函数转换为服务器,包括登录接口路径、请求方式
def login():
username = request.values.get('name') #获取通过url请求传递参数name
pwd = request.values.get('pwd') #注意多个参数时需要用request.values.get()
#获取到之后开始判断name与pwd
if name and pwd:
if name == 'xiaoming' and pwd == '123':
result = {'code':200,'msg':'登录成功'}
return json.dumps(result,ensure_ascii=False) #将json数据转换为字符串,有中文时需要用ensure_ascii=False才能输出真正的中文
else:
result = {'code':-1,'msg':'登录失败,账号密码错误'}
return json.dumps(result,ensure_ascii=False)
else:
result = {'code':10001,'msg':'登录失败,账号密码不能为空'}
return json.dumps(result,ensure_ascii=False)
if __name__ == '__main__':
server1.run(debug=True,port=8888,host='127.0.0.1') #运行服务,设置域名地址为127.0.0.1,端口号8888