使用flask实现一个测试桩

项目路径

testNode/
├── main.py
├── test_node.conf.json
├── tn_commands.py
└── tn_conf.py

配置文件 test_node.conf.json

{
  "host": "127.0.0.1",
  "port": "13499",
  "routers": [
    {
      "service": "service_name",
      "route": {
        "args": [
          "/test"
        ],
        "kwargs": {
          "methods": [
            "GET",
            "POST"
          ],
          "endpoint": "endpoint"
        }
      },
      "response": {
        "headers": {},
        "body": {
          "date_from": "eval",
          "args": ["'hello test.'"],
          "kwargs": {}
        }
      }
    }
  ]
}

tn_conf.py

import json

test_node_obj = None


def get_conf_obj():
    global test_node_obj
    if test_node_obj:
        return test_node_obj
    with open('./test_node.conf.json', 'r') as fd:
        test_node_obj = json.load(fd)
    test_node_obj = type("tn", (object,), test_node_obj)
    return test_node_obj

main.py

from flask import Flask
from tn_conf import get_conf_obj

app = Flask(__name__)
conf_obj = get_conf_obj()

file_content = '''#! /user/bin/python
# -*- coding:utf-8 -*-
from flask import Blueprint, request
import tn_commands
user_bp = Blueprint("蓝图标识", __name__, url_prefix='/')
\n'''

for route in conf_obj.routers:
    args = route["route"]["args"]
    kwargs = route["route"]["kwargs"]
    func = route["response"]["body"]["date_from"]
    func_args = route["response"]["body"]["args"]
    func_kwargs = route["response"]["body"]["kwargs"]
    file_content = f'''{file_content}
@user_bp.route(*{args}, **{kwargs})
def {route["service"]}():
    return {func}(*{func_args}, **{func_kwargs})
\n\n'''

with open("all_route.py", "w", encoding="utf-8") as fd:
    fd.write(file_content)

from all_route import user_bp
app.register_blueprint(user_bp)

if __name__ == '__main__':
    app.run(conf_obj.host, conf_obj.port)

tn_commands.py

from flask import request


def from_file(file, encoding='utf-8'):
    with open(file, mode='r', encoding=encoding) as fd:
        return fd.read()

版权声明:除特别注明外,本站所有文章均为王晨曦个人站点原创

转载请注明:出处来自王晨曦个人站点 » 使用flask实现一个测试桩

点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

  1. test 说道:

    ps -lef | grep http_client | grep -v grep|awk -F' ' '{print $4}' | xargs kill -2

  2. doxosse 说道:

    http://buypropeciaon.com/ - Propecia

  3. antimaNib 说道:

    http://buystromectolon.com/ - ivermectin 3mg for humans

  4. Viagra 说道:

    Cual Es La Mejor Viagra

  5. Propecia 说道:

    Low Cost Pharmacy Online

  6. Adjucky 说道:

    https://buysildenshop.com/ - viagra online fast shipping

  7. visathy 说道:

    https://buytadalafshop.com/ - Cialis

  8. Levitra Quotazione Farmacia

  9. Wheesty 说道:

    http://buyzithromaxinf.com/ - azithromycin moa

  10. dkkzhbobg 说道:

    出发地 dkkzhbobg http://www.gd98t4p96si8r4e0l9to47vd4a9cm302s.org/ adkkzhbobg [url=http://www.gd98t4p96si8r4e0l9to47vd4a9cm302s.org/]udkkzhbobg[/url]

  11. https://bit.ly/3hoC8Vl