@app.route("/",methods=["GET"])
def index():
return "index page"
@app.route("/hello")
def hello():
return "hello"
@app.route("/hey/<username>/")
def hey_yingong(username):
return "hey %s" % (username + username)
@app.route("/my/<float:number>")
def my_number(number):
return "my %s" % (number + number)
@app.route("/baidu")
def baidu():
return redirect("https://www.baidu.com")
@app.route("/test/my/first",methods=["POST","GET"])
def first_post():
try:
my_json = request.get_json()
print(my_json)
get_name=my_json.get("name")
get_age=my_json.get("age")
if not all([get_name, get_age]):
return jsonify(msg="缺参数")
print(get_name)
get_age+=10
return jsonify(name=get_name, age=get_age)
except Exception as e:
print(e)
return jsonify(msg="出错了啊")
#登录
@app.route("/try/login", methods=["POST"])
def login():
get_data = request.get_json()
username = get_data.get("username")
password = get_data.get("password")
if not all([username, password]):
return jsonify(msg="参数不完整")
if username == "admin" and password == "admin":
session["username"]=username
return jsonify(msg="登陆成功")
else:
return jsonify(msg="账号或密码错误")
#检查
@app.route("/session",methods=["GET"])
def check_session():
username = session.get("username")
if username is not None:
return jsonify(username=username)
else:
return jsonify(msg="出错了,没登陆")
#登出
@app.route("/try/logout",methods=["GET"])
def logout():
session.clear()
return jsonify(msg="成功登出")
标签:username,return,get,python,app,jsonify,flask,API,route From: https://www.cnblogs.com/15078480385zyc/p/17337420.html