def connect(self, service, method, headers, body):
        """
        connect
        发起rpc请求,并记录最原始的返回包
        """
        res_dict = ""

        try:
            self.conn.request("POST", service, body, headers)

            response = self.conn.getresponse()
            res = response.read()
            if "<body>" in res or str(res) == "":
                # 返回包不是mcpack形式
                # 分以下两种情况:
                # 1.由于是http请求,所以返回的是一个404之类的html页面,返回结果中有<body>
                #   出现此情况的原因一般是service(如/WinfoModAPIProxy)不存在
                # 2.如果是服务有问题等情况,则返回结果会是空串
                logging.debug("[res: %s]" % (str(res)))
                res_dict = res
            else:
                res_dict = mcpack.loads(res)

            type = "res"
            res_header = response.getheaders()

            self.print_log(service, method, "header", type, str(res_header))
            self.print_log(service, method, "body", type, str(res_dict))

        except Exception as e:
            utils.print_exception(e, __file__)
            res_dict = {}

        return res_dict
Esempio n. 2
0
    def init_conf_service_method(self):
        """
        init conf service & method
        """
        try:
            mod_section = "IdeaModAPI"
            self.service_mod = self.conn.conf.idea_conf.get(
                mod_section, "service")
            self.method_addIdea = self.conn.conf.idea_conf.get(
                mod_section, "method_addIdea")
            self.method_modIdea = self.conn.conf.idea_conf.get(
                mod_section, "method_modIdea")

            query_section = "IdeaQueryAPI"
            self.service_query = self.conn.conf.idea_conf.get(
                query_section, "service")
            self.method_queryIdea = self.conn.conf.idea_conf.get(
                query_section, "method_queryIdea")
        except Exception as e:
            utils.print_exception(e, __file__)
            self.service_mod = globals.DEFAULT_IDEA_SERVICE_MOD
            self.method_addIdea = globals.DEFAULT_IDEA_METHOD_ADD_IDEA
            self.method_modIdea = globals.DEFAULT_IDEA_METHOD_MOD_IDEA

            self.service_query = globals.DEFAULT_IDEA_SERVICE_QUERY
            self.method_queryIdea = globals.DEFAULT_IDEA_METHOD_QUERY_IDEA
Esempio n. 3
0
    def init_conf_service_method(self):
        """
        init conf service & method
        """
        try:
            mod_section = "PlanModAPI"
            self.service_mod = self.conn.conf.plan_conf.get(
                mod_section, "service")
            self.method_addPlan = self.conn.conf.plan_conf.get(
                mod_section, "method_addPlan")
            self.method_modPlan = self.conn.conf.plan_conf.get(
                mod_section, "method_modPlan")
            self.method_modPlanWbudget = \
                    self.conn.conf.plan_conf.get(mod_section, "method_modPlanWbudget")
            self.method_modPlanWregion= \
                    self.conn.conf.plan_conf.get(mod_section, "method_modPlanWregion")
            self.method_modPlanCyc = self.conn.conf.plan_conf.get(
                mod_section, "method_modPlanCyc")

            query_section = "PlanQueryAPI"
            self.service_query = self.conn.conf.plan_conf.get(
                query_section, "service")
            self.method_queryPlan = self.conn.conf.plan_conf.get(
                query_section, "method_queryPlan")
        except Exception as e:
            utils.print_exception(e, __file__)
            self.service_mod = globals.DEFAULT_PLAN_SERVICE_MOD
            self.method_addPlan = globals.DEFAULT_PLAN_METHOD_ADD_PLAN
            self.method_modPlan = globals.DEFAULT_PLAN_METHOD_MOD_PLAN
            self.method_modPlanWbudget = globals.DEFAULT_PLAN_METHOD_MOD_PLAN_WBUDGET
            self.method_modPlanWregion = globals.DEFAULT_PLAN_METHOD_MOD_PLAN_WREGION
            self.method_modPlanCyc = globals.DEFAULT_PLAN_METHOD_MOD_PLAN_CYC

            self.service_query = globals.DEFAULT_PLAN_SERVICE_QUERY
            self.method_queryPlan = globals.DEFAULT_PLAN_METHOD_QUERY_PLAN
 def create_conn(self, url, port, timeout):
     """
     create_conn 建立与服务器的连接
     """
     try:
         conn = httplib.HTTPConnection(url, port, timeout=timeout)
         connectionmgr.ConnectionMgr.set_connection(self, conn)
     except Exception as e:
         utils.print_exception(e, __file__)
         conn = None
    def __init__(self, file):
        """
        init ConfigMgr
        """
        try:
            self.global_conf = configure.Configure(file)

            # 初始化conn_type
            self.conn_type = self.global_conf.get("type", "conn_type")

            # 初始化log_file
            path = self.global_conf.get("log", "path")
            file = self.global_conf.get("log", "file")
            self.log_file = abs_path + path + file

            # 初始化ConnectionGroups
            self.urls = []
            self.ports = []
            self.timeouts = []
            self.conn_group_num = int(
                self.global_conf.get("ConnectionGroupNum", "count"))
            for i in xrange(0, self.conn_group_num):
                group_name = "ConnectionGroup" + str(i)
                url = self.global_conf.get(group_name, "url")
                port = self.global_conf.get(group_name, "port")
                timeout = self.global_conf.get(group_name, "timeout")
                self.urls.append(url)
                self.ports.append(int(port))
                self.timeouts.append(int(timeout))

            # 初始化各model的conf
            path = self.global_conf.get("conf", "path")
            file = self.global_conf.get("conf", "plan")
            plan_file = abs_path + path + file
            self.plan_conf = configure.Configure(plan_file)

            file = self.global_conf.get("conf", "unit")
            unit_file = abs_path + path + file
            self.unit_conf = configure.Configure(unit_file)

            file = self.global_conf.get("conf", "idea")
            idea_file = abs_path + path + file
            self.idea_conf = configure.Configure(idea_file)

            file = self.global_conf.get("conf", "winfo")
            winfo_file = abs_path + path + file
            self.winfo_conf = configure.Configure(winfo_file)

            file = self.global_conf.get("conf", "model")
            model_file = abs_path + path + file
            self.model_conf = configure.Configure(model_file)

        except Exception as e:
            utils.print_exception(e, __file__)
            self.init_default()
Esempio n. 6
0
    def init_conf_service_method(self):
        """
        init conf service & method
        """
        try:
            mod_section = "UnitModAPI"
            self.service_mod = self.conn.conf.unit_conf.get(mod_section, "service")
            self.method_addUnit = self.conn.conf.unit_conf.get(mod_section, "method_addUnit")
            self.method_modUnit = self.conn.conf.unit_conf.get(mod_section, "method_modUnit")

        except Exception as e:
            utils.print_exception(e, __file__)
            self.service_mod = globals.DEFAULT_UNIT_SERVICE_MOD 
            self.method_addUnit = globals.DEFAULT_UNIT_METHOD_ADD_UNIT
            self.method_modUnit = globals.DEFAULT_UNIT_METHOD_MOD_UNIT 
    def process(self, service, method, params):
        """
        process
        总控函数:
        构造请求包->进行连接->分析返回包
        """
        start_time = time.time() * 1000  #ms
        # 构造请求包
        headers = {
            'Content-type': 'application/baidu.mcpack-rpc;charset=GBK',
            'Connection': "close"
        }

        body = self.make_body(method, params)

        # 填充header和body
        connectionmgr.ConnectionMgr.prepare(self, headers, body)

        type = "req"
        self.print_log(service, method, "header", type,
                       str(json.dumps(headers)))
        self.print_log(service, method, "body", type, str(json.dumps(body)))

        try:
            # mcpack.dumps()只接受字典类型,如果body不是字典,会抛异常
            mybody = mcpack.dumps(body)
        except Exception as e:
            utils.print_exception(e, __file__)
            mybody = {}
            res = self.analyse_response(mybody)
            return res

        start_conn_time = time.time() * 1000  #ms
        # 连接server
        response = self.connect(service, method, headers, mybody)
        end_conn_time = time.time() * 1000  #ms
        conn_proc_time = end_conn_time - start_conn_time
        logging.info("[method:%s][conn proc_time:%fms]" %
                     (method, conn_proc_time))

        # 分析返回包
        res = self.analyse_response(response)
        end_time = time.time() * 1000  # ms
        proc_time = end_time - start_time
        logging.info("[method:%s][total proc_time:%fms]" % (method, proc_time))

        return res
Esempio n. 8
0
    def init_conf_service_method(self):
        """
        init conf service & method
        """
        try:
            mod_section = "WinfoModAPI"
            self.service_mod = self.conn.conf.winfo_conf.get(
                mod_section, "service")
            self.method_addWinfo = self.conn.conf.winfo_conf.get(
                mod_section, "method_addWinfo")
            self.method_modWinfo = self.conn.conf.winfo_conf.get(
                mod_section, "method_modWinfo")
            self.method_modWinfoPause = \
                    self.conn.conf.winfo_conf.get(mod_section, "method_modWinfoPause")
            self.method_modWinfoBid = \
                    self.conn.conf.winfo_conf.get(mod_section, "method_modWinfoBid")
            self.method_modWinfoWmatch = \
                    self.conn.conf.winfo_conf.get(mod_section, "method_modWinfoWmatch")
            self.method_modWinfoWctrl = \
                    self.conn.conf.winfo_conf.get(mod_section, "method_modWinfoWctrl")
            self.method_delWinfo = self.conn.conf.winfo_conf.get(
                mod_section, "method_delWinfo")

            query_section = "WinfoQueryAPI"
            self.service_query = self.conn.conf.winfo_conf.get(
                query_section, "service")
            self.method_queryWinfo = \
                    self.conn.conf.winfo_conf.get(query_section, "method_queryWinfo")
        except Exception as e:
            utils.print_exception(e, __file__)
            self.service_mod = globals.DEFAULT_WINFO_SERVICE_MOD
            self.method_addWinfo = globals.DEFAULT_WINFO_METHOD_ADD_WINFO
            self.method_modWinfo = globals.DEFAULT_WINFO_METHOD_MOD_WINFO
            self.method_modWinfoPause = globals.DEFAULT_WINFO_METHOD_MOD_WINFO_PAUSE
            self.method_modWinfoBid = globals.DEFAULT_WINFO_METHOD_MOD_WINFO_BID
            self.method_modWinfoWmatch = globals.DEFAULT_WINFO_METHOD_MOD_WINFO_WMATCH
            self.method_modWinfoWctrl = globals.DEFAULT_WINFO_METHOD_MOD_WINFO_WCTRL
            self.method_delWinfo = globals.DEFAULT_WINFO_METHOD_DEL_WINFO

            self.service_query = globals.DEFAULT_WINFO_SERVICE_QUERY
            self.method_queryWinfo = globals.DEFAULT_WINFO_METHOD_QUERY_WINFO