python+charles远程映射实现本地mock

Saisco

python代码:

# post请求
@local_service.route('/serverName/postInterfaceName', methods=['POST'])
@cross_origin(supports_credentials=True)
def post_interface_name():
    # #读取json文件当成返回
    path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/jsonFile/jsonFileName.json'
    with open(path, 'r', encoding='utf')as fp:
        response = json.load(fp)
    return response


# get请求
@local_service.route('/system/time', methods=['GET'])
@cross_origin(supports_credentials=True)
def get_interface_name():
    time = {"success": True, "code": "8000", "result":  1625068800000}
    time_map = json.dumps(time, sort_keys=True, indent=4, separators=(',', ': '))
    return time_map


if __name__ == '__main__':
    local_service.run(port=8890, debug=True, host='127.0.0.1')

charles设置:
右键要mock的接口--->Map remote--->选择Protocol,填写Host Port Path--->OK
image.png

每次系统调用这个接口都会走本地mock数据

阅读 244
1 声望
1 粉丝
0 条评论
你知道吗?

1 声望
1 粉丝
宣传栏