zoukankan      html  css  js  c++  java
  • 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)
    

      

  • 相关阅读:
    CodeForces 383C-dfs序-线段树
    poj-3321-dfs序-线段树-邻接表
    poj2528-Mayor's posters-线段树离散化、基础
    hdu3333-Turing Tree-线段树+离线+离散化
    poj 1151-atlantis-线段树扫描线求面积并
    Changes favor the connective minds.
    HDU 4800/zoj 3735 Josephina and RPG 2013 长沙现场赛J题
    HDU 1203 I NEED A OFFER! 01背包
    hdu 1175 连连看 DFS
    Codeforces Round #208 (Div. 2) 358D Dima and Hares
  • 原文地址:https://www.cnblogs.com/lly-lcf/p/13358876.html
Copyright © 2011-2022 走看看