from http.client import SWITCHING_PROTOCOLS from flask import Flask, request, redirect, url_for, render_template class switch_case(object): def case_to_function(self, case): fun_name = "case_fun_" + str(case) method = getattr(self, fun_name, self.case_fun_other) return method def case_fun_start(self, msg): print(msg) def case_fun_stop(self, msg): print(msg) def case_fun_other(self, msg): print(msg) # 创建Flask服务 app = Flask(__name__) # 访问URL:http://127.0.0.1:8080/home/hello # 返回结果:{"data":"welcome to use flask.","msg":"hello"} @app.route('/home/') def home(name): cls.case_to_function(name)("case_fun_"+name+" is called") return { "msg": name, "data": "welcome to use flask." } if __name__ == "__main__": cls = switch_case() # 启动Flask服务,指定主机IP和端口 app.run(host='127.0.0.1', port=8080)