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

Posted botoo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

 

暂时记要

 

以上是关于python基础===将Flask用于实现Mock-server的主要内容,如果未能解决你的问题,请参考以下文章

python-flask框架&mock接口开发

python-flask框架&mock接口开发

Python+Flask搭建mock api server

Python之mock接口开发

Python之mock接口开发

python小记13:mock接口开发1