Exemplo n.º 1
0
    def __init__(self,
                 server,
                 thriftmod,
                 timeout=0,
                 framed=False,
                 raise_except=False):
        '''server - 为Selector对象,或者地址{'addr':('127.0.0.1',5000),'timeout':1000}'''
        global etcd_cache
        self.starttime = time.time()
        self.server_selector = None
        self.server = None
        self.client = None
        self.thriftmod = thriftmod
        self.frame_transport = framed
        self.raise_except = raise_except  # 是否在调用时抛出异常

        self.timeout = timeout

        if isinstance(server, dict):  # 只有一个server
            self.server = [
                server,
            ]
            self.server_selector = selector.Selector(self.server, 'random')
        elif isinstance(server, list):  # server列表,需要创建selector,策略为随机
            self.server = server
            self.server_selector = selector.Selector(self.server, 'random')
        #elif isinstance(server, str) or isinstance(server, unicode):
        #    self.server = etcd_cache[server]
        else:  # 直接是selector
            self.server_selector = server
        while True:
            if self.open() == 0:
                break
Exemplo n.º 2
0
    def __init__(self,
                 server,
                 protocol='http',
                 timeout=0,
                 raise_except=False,
                 log_except=True,
                 client_class=Urllib2Client):
        self.server_selector = None
        self.protocol = protocol
        self.timeout = timeout
        self.client_class = client_class
        self.client = None
        self.server = None
        self.raise_except = raise_except  # 是否在调用时抛出异常
        self.log_except = log_except  # 是否打日志

        if isinstance(server, dict):  # 只有一个server
            self.server = [
                server,
            ]
            self.server_selector = selector.Selector(self.server, 'random')
        elif isinstance(server, list):  # server列表,需要创建selector,策略为随机
            self.server = server
            self.server_selector = selector.Selector(self.server, 'random')
        #elif isinstance(server, str) or isinstance(server, unicode):
        #    self.server = etcd_cache[server]
        else:  # 直接是selector
            self.server_selector = server

        #如果无可用 尝试恢复
        if len(self.server_selector.valid()) == 0:
            http_restore(self.server_selector, self.protocol)
Exemplo n.º 3
0
def test():
    from qfcommon.thriftclient.payprocessor import PayProcessor
    from qfcommon.base import logger
    global log
    logger.install('stdout')
    log = logger.log
    log.debug('test ...')
    serverlist = [
        {
            'addr': ('127.0.0.1', 4300),
            'timeout': 1000
        },
        {
            'addr': ('127.0.0.1', 4200),
            'timeout': 1000
        },
    ]
    sel = selector.Selector(serverlist)
    for i in range(0, 10):
        client = ThriftClient(sel, PayProcessor)
        client.ping()

    server = sel.next()
    server['valid'] = False

    #log.debug('restore ...')
    #restore(sel)
    print '-' * 60
    for i in range(0, 10):
        client = ThriftClient(sel, PayProcessor)
        client.ping()
Exemplo n.º 4
0
def test2():
    from qfcommon.thriftclient.encryptor import Encryptor
    from qfcommon.base import logger
    global log
    logger.install('stdout')
    log = logger.log
    log.debug('test ...')
    serverlist = [
        {
            'addr': ('127.0.0.1', 4200),
            'timeout': 1000
        },
        {
            'addr': ('127.0.0.1', 4201),
            'timeout': 1000
        },
    ]
    sel = selector.Selector(serverlist)
    for i in range(0, 10):
        client = ThriftClient(sel, Encryptor)
        client.ping()
Exemplo n.º 5
0
def test3():
    from qfcommon.thriftclient.notifier import Notifier
    from qfcommon.base import logger
    global log
    logger.install('stdout')
    log.debug("test framed transport")
    serverlist = [
        {
            'addr': ('172.100.101.151', 15555),
            'timeout': 1000
        },
    ]
    sel = selector.Selector(serverlist)
    client = ThriftClient(sel, Notifier, framed=True)
    notify = {
        "notify_url": "http://172.100.101.151:8989/",
        "notify_data": {
            "orderstatus": "5",
        }
    }
    import json
    ret = client.send_notify(json.dumps(notify))
    log.debug("send notify return:%s", ret)