cshenfu
2023-03-23 71ee526926c17d79fd350a04f949940ef130d2a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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/<name>')
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)