python基础===将Flask用于实现Mock-server

 

 

 

from flask import Flask
from flask import request, Response, jsonify
import random
import string


app = Flask(__name__)


def id_generator(size=20,
                 chars=string.digits + string.ascii_letters):
    """
    定义了一个
    """

    str_ = ""

    for i in range(size):
        i = random.choice(chars)
        str_ += str(i)

    return str_


def response():
    content = '{"result": "%s", "data": "%s"}' % (
        id_generator(4), id_generator(9))
    resp = Response(content)
    resp.headers["Access-Control-Origin"] = '*'

    return resp


@app.route("/")  # 路由
def index():
    return "welcome!"


# http get
@app.route("/query", methods=["GET"])
def query():

    pass

    return jsonify(
        username=id_generator(4),
        password=id_generator(9),
    )


@app.route("/update", methods=["POST"])
def update():

    return response()


@app.route("/delete", methods=["DELEDT"])
def delete():

    return response()


@app.route("/head", methods=['HEAD'])
def head():

    return response()


if __name__ == '__main__':
    app.run(debug=True)

 

暂时记要

 

posted @ 2018-05-11 17:16  AAA五金批发王建军  阅读(672)  评论(0编辑  收藏  举报