Пример #1
0
 def send_result(self):
     """
     send data to branch_center_list, get result
     :param branch_center_list:
     :param body:
     :param path:
     :param connect_timeout:
     :param response_timeout:
     :return:
     """
     result_list = []
     clients = []
     my_map = {}
     # path = "/probetask"
     for dev in self.body:
         logger.debug("dev is %s" % dev)
         try:
             dev_temp = dev.split(":")
             subcenter_host = dev_temp[0]
             subcenter_port = int(dev_temp[1])
             if self.is_compressed == True:
                 body_send = zlib.compress(json.dumps(self.body[dev]))
             else:
                 body_send = json.dumps(self.body.get(dev))
             if body_send:
                 clients.append(
                     HttpClient(subcenter_host, subcenter_port, body_send,
                                len(body_send), my_map,
                                self.connect_timeout,
                                self.response_timeout))
         except Exception, ex:
             logger.debug("send content error :%s" %
                          traceback.format_exc(ex))
Пример #2
0
def doloop_test_send(devs, url, connect_timeout=1.5, response_timeout=1.5):
    # def doloop(devs, command, connect_timeout=1.5, response_timeout=10):
    """
     调用asyncore,创建信道,与FC 通过socket连接,端口21108
    :param devs:
    :param command:
    :return:
    """
    clients = []
    ret = []
    ret_faild = []
    my_map = {}
    port = 21108
    for dev in devs:
        command = getUrlCommand(url)
        clients.append(
            HttpClient(dev['host'], port, command,
                       len(devs) * 200, my_map, connect_timeout,
                       response_timeout))
    logger.debug('LOOP STARTING.')
    # asyncore.loop(timeout=response_timeout, map=my_map)#select()或poll()调用设置超时,以秒为单位,默认为30秒
    # asyncore.loop(timeout=0.5, use_poll=True, map=my_map)#select()或poll()调用设置超时,以秒为单位,默认为30秒
    # modify the business logic here, use two methods to deal with , for number of devices  more than 300 in a way to
    #  deal with, and the number of devices less than 300 another way to deal with
    if len(devs) < 300:
        #asyncore.loop(timeout=response_timeout, map=my_map)
        asyncore.loop(timeout=0.1, map=my_map)
    else:
        #asyncore.loop(timeout=response_timeout, use_poll=True, map=my_map)
        asyncore.loop(timeout=0.1, use_poll=True, map=my_map)
    logger.debug('LOOP DONE.')
    for c in clients:
        response_body, total_cost, connect_cost, response_cost, response_code = c.read_buffer.getvalue(
        ), c.total_cost, c.connect_cost, c.response_cost, c.response_code
        if response_body:
            try:
                ret.append(
                    c.host + '\r\n' + response_body.split('\r\n\r\n')[1] +
                    '\r\n%d\r\n%.2f\r\n%.2f\r\n%.2f' %
                    (response_code, total_cost, connect_cost, response_cost))
                # logger.warn("devs: %s doloop response_body: %s" % (devs, response_body))
                logger.debug(
                    "have response_body host:%s, response_code:%s, response_body:%s"
                    % (c.host, response_code, response_body))
            except Exception, e:
                # logger.error("devs: %s doloop response_body error: %s" % (devs, response_body))
                logger.error("doloop error: %s" % traceback.format_exc(e))
        else:
            logger.debug("not have response_body host:%s, response_code:%s" %
                         (c.host, response_code))
            ret_faild.append(
                c.host + '\r\n%d\r\n%.2f\r\n%.2f\r\n%.2f' %
                (response_code, total_cost, connect_cost, response_cost))
            if c.strerror != 'no_error':
                blackListDB.set(c.host, c.strerror)
                blackListDB.expire(c.host, 300)
Пример #3
0
def doloop(devs, tasks, connect_timeout=5, response_timeout=8):
    """
    调用asyncore,创建信道,与设备通过socket连接,端口61108
    :param devs:
    :param command:
    :return:
    """
    clients = []
    # http_results = []
    my_map = {}
    port = 51108
    pre_ret = []
    pre_ret_faild = []

    command = zlib.compress(get_command(tasks))

    for dev in devs:
        clients.append(
            HttpClient(dev['host'], port, command,
                       len(devs) * 200, my_map, connect_timeout,
                       response_timeout))

    logger.debug('LOOP STARTING.')
    asyncore.loop(timeout=0.1, use_poll=True, map=my_map)
    logger.debug('LOOP DONE.')

    for c in clients:

        response_body, total_cost, connect_cost, response_cost, response_code= c.read_buffer.getvalue(), \
                                                        c.total_cost, c.connect_cost, c.response_cost, c.response_code,

        if response_body:
            try:
                pre_ret.append(
                    c.host + '\r\n' + response_body.split('\r\n\r\n')[1] +
                    '\r\n%d\r\n%.2f\r\n%.2f\r\n%.2f' %
                    (response_code, total_cost, connect_cost, response_cost))
                # logger.warn("devs: %s doloop response_body: %s" % (devs, response_body))
                logger.debug(
                    "pre_host_test:%s, response_code_test:%s, response_body:%s"
                    % (c.host, response_code, response_body))
            except Exception, e:
                logger.error("pre_devs: %s doloop response_body error: %s" %
                             (devs, response_body))
                logger.error("doloop error: %s" % traceback.format_exc(e))
        else:
            # handling the case without response_body
            pre_ret_faild.append(
                c.host + '\r\n%d\r\n%.2f\r\n%.2f\r\n%.2f' %
                (response_code, total_cost, connect_cost, response_cost))
Пример #4
0
def doloop(devs,
           urls,
           connect_timeout=1.5,
           response_timeout=1.5,
           first_layer_preload_len=0,
           test=0):
    """
    调用asyncore,创建信道,与FC 通过socket连接,端口31108
    :param devs:
    :param command:
    :return:
    """
    clients = []
    # http_results = []
    my_map = {}
    port = 31108
    pre_ret = []
    pre_ret_faild = []
    for dev in devs:
        # clients.append(HttpClient(dev.get('host'), port, command, len(devs) * 200, my_map))
        logger.debug("doloop first_layer_preload_len:%s, firstLayer:%s" %
                     (first_layer_preload_len, dev.get('firstLayer')))
        if first_layer_preload_len:
            if dev.get('firstLayer'):
                clients.append(
                    HttpClient(
                        dev.get('host'), port,
                        get_command(urls,
                                    urls[0].get("action"),
                                    dev.get('host'),
                                    conn_num=urls[0].get('conn_num'),
                                    speed=urls[0].get('single_limit_speed'),
                                    test=test),
                        len(devs) * 200, my_map, connect_timeout,
                        response_timeout))
            else:
                clients.append(
                    HttpClient(
                        dev.get('host'), port,
                        get_command(urls,
                                    urls[0].get("action"),
                                    dev.get('host'),
                                    test=test),
                        len(devs) * 200, my_map, connect_timeout,
                        response_timeout))
        else:
            clients.append(
                HttpClient(
                    dev.get('host'), port,
                    get_command(urls,
                                urls[0].get("action"),
                                dev.get('host'),
                                conn_num=urls[0].get('conn_num'),
                                speed=urls[0].get('single_limit_speed'),
                                test=test),
                    len(devs) * 200, my_map, connect_timeout,
                    response_timeout))

    logger.debug('LOOP STARTING.')
    # asyncore.loop(timeout=0.5, map=my_map)
    # asyncore.loop(timeout=0.5, use_poll=True, map=my_map)
    if len(devs) < 300:
        #asyncore.loop(timeout=response_timeout, map=my_map)
        asyncore.loop(timeout=0.1, map=my_map)
    else:
        #asyncore.loop(timeout=response_timeout, use_poll=True, map=my_map)
        asyncore.loop(timeout=0.1, use_poll=True, map=my_map)
    logger.debug('LOOP DONE.')
    for c in clients:

        response_body, total_cost, connect_cost, response_cost, response_code= c.read_buffer.getvalue(), \
                                                        c.total_cost, c.connect_cost, c.response_cost, c.response_code,

        if response_body:
            try:
                pre_ret.append(
                    c.host + '\r\n' + response_body.split('\r\n\r\n')[1] +
                    '\r\n%d\r\n%.2f\r\n%.2f\r\n%.2f' %
                    (response_code, total_cost, connect_cost, response_cost))
                # logger.warn("devs: %s doloop response_body: %s" % (devs, response_body))
                logger.debug(
                    "pre_host_test:%s, response_code_test:%s, response_body:%s"
                    % (c.host, response_code, response_body))
            except Exception, e:
                logger.error("pre_devs: %s doloop response_body error: %s" %
                             (devs, response_body))
                logger.error("doloop error: %s" % traceback.format_exc(e))
        else:
            # handling the case without response_body
            pre_ret_faild.append(
                c.host + '\r\n%d\r\n%.2f\r\n%.2f\r\n%.2f' %
                (response_code, total_cost, connect_cost, response_cost))
Пример #5
0
def doloop(devs, urls, connect_timeout=5, response_timeout=8):
    """
    调用asyncore,创建信道,与FC 通过socket连接,端口31108
    :param devs:
    :param command:
    :return:
    """
    clients = []
    # http_results = []
    my_map = {}
    port = 31108
    pre_ret = []
    pre_ret_faild = []
    have_first_layer = any([d.get('firstLayer') for d in devs])
    for dev in devs:
        # clients.append(HttpClient(dev.get('host'), port, command, len(devs) * 200, my_map))
        logger.debug("doloop have_first_layer: %s|| firstLayer: %s" %
                     (have_first_layer, dev.get('firstLayer')))
        if have_first_layer:
            if dev.get('firstLayer'):
                clients.append(
                    HttpClient(
                        dev.get('host'), port,
                        get_command_json(urls,
                                         urls[0].get("action"),
                                         dev,
                                         check_conn=True),
                        len(devs) * 200, my_map, connect_timeout,
                        response_timeout))
            else:
                clients.append(
                    HttpClient(
                        dev.get('host'), port,
                        get_command_json(urls, urls[0].get("action"), dev),
                        len(devs) * 200, my_map, connect_timeout,
                        response_timeout))
        else:
            clients.append(
                HttpClient(
                    dev.get('host'), port,
                    get_command_json(urls,
                                     urls[0].get("action"),
                                     dev,
                                     check_conn=True),
                    len(devs) * 200, my_map, connect_timeout,
                    response_timeout))

    logger.debug('doloop LOOP STARTING.')
    asyncore.loop(timeout=0.1, use_poll=True, map=my_map)
    logger.debug('doloop LOOP DONE.')
    for c in clients:
        response_body, total_cost, connect_cost, response_cost, response_code = c.read_buffer.getvalue(), \
                                                    c.total_cost, c.connect_cost, c.response_cost, c.response_code
        if response_body:
            try:
                pre_ret.append(
                    c.host + '\r\n' + response_body.split('\r\n\r\n')[1] +
                    '\r\n%d\r\n%.2f\r\n%.2f\r\n%.2f' %
                    (response_code, total_cost, connect_cost, response_cost))
                # logger.warn("devs: %s doloop response_body: %s" % (devs, response_body))
                logger.debug(
                    "doloop pre_host_test: %s|| response_code_test: %s|| response_body: %s"
                    % (c.host, response_code, response_body))
            except Exception, e:
                logger.error("doloop pre_devs: %s|| response_body error: %s" %
                             (devs, response_body))
                logger.error("doloop error: %s" % traceback.format_exc(e))
        else:
            # handling the case without response_body
            pre_ret_faild.append(
                c.host + '\r\n%d\r\n%.2f\r\n%.2f\r\n%.2f' %
                (response_code, total_cost, connect_cost, response_cost))