python-接口编写-get和post,真假接口


	python-接口编写-get和post,真假接口
[编程语言教程]

from flask import request
from flask import Flask
from flask import jsonify
app = Flask(__name__)
from unittest_1.common.common import mysql

app.config["JSON_AS_ASCII"] = False  # jsonify返回的中文正常显示

@app.route(‘/path‘,methods = [‘get‘])
def test():
  return jsonify({"code": "200", "data": ‘这是个假接口‘, "msg": "处理成功"})
@app.route(‘/phone‘,methods = [‘get‘])
def find_phone():#查看手机号是否被使用
    db=mysql(‘express_auth‘)
    cu = db.cursor()  # 这是生成的一个操作数据库的对象
    username = request.json.get("username").strip()
    sql=‘select user_name from express_auth where user_name=‘+username
    cu.execute(sql)  # 执行sql


@app.route("/register", methods=[‘POST‘])
def user_register():
    username = request.json.get("username").strip() # 用户名       这个json是设置请求参数必须以josn格式上传
    password = request.json.get("password").strip() # 密码
    telephone = request.json.get("telephone", "").strip() # 手机号,默认为空
    address = request.json.get("telphone", "").strip() # 地址,默认为空
    if username and password and telephone:
        import re
        if username == "wintest":
            return jsonify({"code": 401, "msg": "用户名已存在!!!"})
        elif not(len(telephone) == 11 and re.match("re.compile(r‘^1(3d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8[0-9]|9[0-9])d{8}$‘)", telephone)):
            return jsonify({"code": 403, "msg": "手机号格式不正确!!!"})
        else:
            return jsonify({"code": 200, "msg": "恭喜,注册成功!"})
    else:
        return jsonify({"code": 404, "msg": "用户名/密码/手机号不能为空,请检查!!!"})

@app.route("/login", methods=[‘POST‘])
def user_login():
    username = request.values.get("username")
    password = request.values.get("password")
    if username and password:
        if username == "wintest" and password == "123456":
            return jsonify({"code": 0, "msg": "恭喜,登录成功!"})
        return jsonify({"code": 1002, "msg": "用户名或密码错误!!!"})
    else:
        return jsonify({"code": 1001, "msg": "用户名或密码不能为空!!!"})

# @app.route("/login",methods = [‘POST‘,‘GET‘])
# def login():
#   if request.method == "POST":
#     print(1)
#     username = request.form.get(‘username‘)
#     username_str=str(username)
#     print(username_str)
#     # if username_str==‘李超凡‘:
#     if 1==1:
#         password = request.form.get(‘password‘)
#         if password==‘123456‘:
#             return ‘登录成功‘
#         else:
#             return ‘密码错误‘
#     else:
#         return ‘用户名不正确‘
#   if request.method == "GET":
#     username = request.args.get(‘username‘)
#     password = request.args.get(‘password‘)
#     return usernam
if __name__ == ‘__main__‘:
    app.run(debug=True,host=‘192.168.1.130‘,port=8123)
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » python-接口编写-get和post,真假接口