image_server_list.py 1.6 KB
# coding=utf-8
#author:        4N
#createtime:    2021/7/19
#email:         nheweijun@sina.com

from app.util.component.ApiTemplate import ApiTemplate

from kazoo.client import KazooClient
import configure
class Api(ApiTemplate):

    api_name = "获取数据服务器列表"

    def process(self):
        from app import GLOBAL_DIC

        # 返回结果
        res = {}
        try:
            zoo = GLOBAL_DIC.get("zookeeper")
            if zoo is None:
                zoo :KazooClient = KazooClient(hosts=configure.zookeeper, timeout=100)
                zoo.start()
                GLOBAL_DIC["zookeeper"] = zoo
            else :
                if not zoo.connected:
                    zoo.start()
            sers = zoo.get_children("/rpc")
            res["data"] = []
            for p in sers:
                bl = str( zoo.get("/rpc/{}".format(p))[0] , encoding = "utf-8")
                res["data"].append({"name": p, "belong": bl})

            res["data"].append({"name":"本地服务器","belong":""})
            res["result"] = True
        except Exception as e:
            raise e
        return res

    api_doc = {
        "tags": ["影像接口"],
        "parameters": [
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }

if __name__ == '__main__':
    zoo: KazooClient = KazooClient(hosts="172.26.99.168:2181", timeout=100)
    zoo.start()
    print(zoo.get_children("/rpc"))
    zoo.stop()
    zoo.close()