def main(): with client_context(dd_thrift.DingService, '127.0.0.1', 9090) as c: # ring that doorbell dong = c.ding() print(dong) with client_context(pp_thrift.PingService, '127.0.0.1', 9090) as c: # play table tennis like a champ pong = c.ping() print(pong)
def main(): binary_factory = TBinaryProtocolFactory() dd_factory = TMultiplexedProtocolFactory(binary_factory, DD_SERVICE_NAME) with client_context(dd_thrift.DingService, "127.0.0.1", 9090, proto_factory=dd_factory) as c: # ring that doorbell dong = c.ding() print(dong) pp_factory = TMultiplexedProtocolFactory(binary_factory, PP_SERVICE_NAME) with client_context(pp_thrift.PingService, "127.0.0.1", 9090, proto_factory=pp_factory) as c: # play table tennis like a champ pong = c.ping() print(pong)
def client_two(timeout=3000): binary_factory = TBinaryProtocolFactory() multiplexing_factory = TMultiplexedProtocolFactory(binary_factory, "ThingTwoService") return client_context(mux.ThingTwoService, unix_socket=sock_path, timeout=timeout, proto_factory=multiplexing_factory)
def bg_thrift_context(sync=False, **kwargs): with client_context(bg_utils.bg_thrift.BartenderBackend, host=config.backend.host, port=config.backend.port, socket_timeout=config.backend.socket_timeout, **kwargs) as client: yield client if sync else BgClient(client)
def main(): with client_context(calc_thrift.Calculator, '127.0.0.1', 6000) as cal: time.time() time_sq = [] time_eight = [] time_upper = [] time_void = [] for i in range(5): inicio = time.clock() response = cal.SquareRoot(98881111) print(response) final = time.clock() - inicio time_sq.append(final) inicio = time.clock() response = cal.UpperCase( 'Testando string para materia de Sistemas Distribuidos') final = time.clock() - inicio time_upper.append(final) inicio = time.clock() response = cal.SquareEight(2, 4, 8, 16, 32, 64, 128, 256) final = time.clock() - inicio time_eight.append(final) inicio = time.clock() print(inicio) cal.ExecVoid() final = time.clock() - inicio time_void.append(final) def show(time): print(time) plt.plot(time) plt.ylabel('times') plt.show() print("Media:") print(numpy.mean(time)) print("Desvio Padrão:") print(numpy.std(time)) print("") print("Square Root:") plt.xlabel('square') show(time_sq) print("Upper:") plt.xlabel('upper') show(time_upper) print("Square Eight:") plt.xlabel('square eight') show(time_eight) print("Void:") plt.xlabel('void') show(time_void)
def main(): with client_context(tutorial_thrift.Calculator, '127.0.0.1', 6000) as client: client.ping() print("ping()") total = client.add(1, 1) print('1+1=%d' % (total)) work = tutorial_thrift.Work() work.op = tutorial_thrift.Operation.DIVIDE work.num1 = 1 work.num2 = 0 try: client.calculate(1, work) print('Whoa? You know how to divide by zero?') except tutorial_thrift.InvalidOperation as io: print('InvalidOperation: %r' % io) work.op = tutorial_thrift.Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = client.calculate(1, work) print('15-10=%d' % (diff)) log = client.getStruct(1) print('Check log: %s' % (log.value))
def main(): with client_context(tutorial.Calculator, '127.0.0.1', 6000, proto_factory=TBinaryProtocolFactory()) as client: client.ping() print("ping()") total = client.add(1, 1) print('1+1=%d' % (total)) work = tutorial.Work() work.op = tutorial.Operation.DIVIDE work.num1 = 1 work.num2 = 0 try: client.calculate(1, work) print('Whoa? You know how to divide by zero?') except tutorial.InvalidOperation as io: print('InvalidOperation: %r' % io) work.op = tutorial.Operation.SUBTRACT work.num1 = 15 work.num2 = 10 diff = client.calculate(1, work) print('15-10=%d' % (diff)) log = client.getStruct(1) print('Check log: %s' % (log.value))
def main(): args = parse() args_dict = dict(args.__dict__) logsetup(args_dict) try: # create thrift client with client_context(tracelogs_thrift.TraceLogService, args.thrift_server, args.thrift_port, proto_factory=TCyBinaryProtocolFactory(), trans_factory=TCyBufferedTransportFactory() ) as thrift_client: client = TracesThriftClientController(client=thrift_client, module=tracelogs_thrift) output = client.dispatchCall(args.command, args) print output except thriftpy.thrift.TException as ex: logger.error('Thrift error: %s' % ex.message) except socket_error as sex: logger.error('Network error: %s' % sex) except Exception as ex: logger.error('Application error: %s' % ex) raise
def ssl_client(timeout=3000): return client_context(addressbook.AddressBookService, host='localhost', port=SSL_PORT, timeout=timeout, cafile="ssl/CA.pem", certfile="ssl/client.crt", keyfile="ssl/client.key")
def client(thrift, host, port, service): calc_thrift = thriftpy.load(str(thrift)) with client_context(getattr(calc_thrift, service), host, port, proto_factory=TCyBinaryProtocolFactory(), trans_factory=TCyBufferedTransportFactory()) as client: IPython.embed(header="Call client.api(*args)")
def request_thrift(thrift_name, service, method, url, port, *args, **kwargs): ''' 通用的 simpled rpc client ''' module_name = thrift_name.replace('.', '_') thrift_module = thriftpy.load(thrift_name, module_name=module_name) thrift_service = getattr(thrift_module, service) with client_context(thrift_service, url, port) as rt: res = getattr(rt, method)(*args, **kwargs) print(res)
def main(): ''' ''' example_thrift = thriftpy.load("rpc_example.thrift", module_name="pp_thrift") with client_context(example_thrift.example, '127.0.0.1', 6000) as c: print c.ping()
def rpc_seg(text): result = "" with client_context(hanlprpc_thrift.Segmenter, HANLPRPC_HOST, HANLPRPC_PORT) as client: result = client.seg(text) time.sleep(0.2) return result
def make_client(host=None, port=None): host = host or KOENIG_THRIFT_SETTINGS['host'] port = port or KOENIG_THRIFT_SETTINGS['port'] return client_context( koenig_thrift.KoenigService, host, port )
def thrift_client(self, service_name): if not self._services.get(service_name): self._services[service_name] = _load_service(service_name) return client_context( self._services[service_name].t_service, config.THRIFT_SERVICE[service_name]['host'], config.THRIFT_SERVICE[service_name]['port'] )
def request_thrift(thrift_name,service, method, url, port, *args, **kwargs): ''' 通用的 multiplexed ''' module_name = thrift_name.replace('.', '_') thrift_module = thriftpy.load(thrift_name, module_name=module_name) thrift_service = getattr(thrift_module, service) binary_factory = TBinaryProtocolFactory() thrift_factory = TMultiplexedProtocolFactory(binary_factory, module_name) with client_context(thrift_service, url, port, proto_factory=thrift_factory) as rt: res = getattr(rt, method)(*args, **kwargs) print(res)
def main(): binary_factory = TBinaryProtocolFactory() dd_factory = TMultiplexedProtocolFactory(binary_factory, DD_SERVICE_NAME) with client_context(dd_thrift.DingService, '127.0.0.1', 9090, proto_factory=dd_factory) as c: # ring that doorbell dong = c.ding() print(dong) pp_factory = TMultiplexedProtocolFactory(binary_factory, PP_SERVICE_NAME) with client_context(pp_thrift.PingService, '127.0.0.1', 9090, proto_factory=pp_factory) as c: # play table tennis like a champ pong = c.ping() print(pong)
def main(): with client_context(calc.Calculator, '127.0.0.1', 6000, proto_factory=TCyBinaryProtocolFactory()) as cal: a = cal.mult(5, 2) b = cal.sub(7, 3) c = cal.sub(6, 4) d = cal.mult(b, 10) e = cal.add(a, d) f = cal.div(e, c) print(f)
def thrift(request): with client_context(calc_thrift.Calculator, '127.0.0.1', 6000, proto_factory=TCyBinaryProtocolFactory(), trans_factory=TCyBufferedTransportFactory()) as cal: a = cal.mult(5, 2) b = cal.sub(7, 3) c = cal.sub(6, 4) d = cal.mult(b, 10) e = cal.add(a, d) f = cal.div(e, c) return Response(str(b))
def ping_server_at_ip(ip): # type: (str) -> (str, bool) try: with client_context(pepi_thrift.CameraServer, ip, 6000, connect_timeout=300, socket_timeout=1000) as c: ping_result = c.ping() except thriftpy.transport.TTransportException: return ip, False else: return ip, ping_result
def set_device_online(device_id): ret = SERVICES_ONLINE if len(ret) < 1: # pass raise Exception("没有可用的服务:%s" % SERVICES_ONLINE) # 随机方式实现负载均衡 server = ret[randint(0, len(ret) - 1)].split(":") # server_addr = "122.144.167.74" # server_port = 48076 server_addr = server[0] server_port = int(server[1]) try: with client_context(THRIFT_ONLINE.Device, server_addr, server_port, timeout=None, socket_timeout=20000) as client: r = client.heartbeat(device_id, 300) return r except Exception as e: logging.exception(e) return None
def call(self, funcname, *args, **kwargs): def _call_log(ret, err=''): endtime = time.time() addr = self.server['server']['addr'] tname = self.service.__name__ pos = tname.rfind('.') if pos > 0: tname = tname[pos + 1:] retstr = str(ret) if tname == 'Encryptor' and ret: retstr = str(ret.code) s = 'server=%s|func=%s|addr=%s:%d/%d|time=%d|args=%s|kwargs=%s' %\ (tname, funcname, addr[0], addr[1], self.server['server']['timeout'], int((endtime - starttime) * 1000000), args[1:len(args) - 1], kwargs) if err: s += '|ret=%s|err=%s' % (retstr, repr(err)) log.warn(s) else: log.info(s) starttime = time.time() ret = None try: addr = self.server['server']['addr'] with client_context( self.service, addr[0], addr[1], timeout=self.server['server']['timeout']) as client: func = getattr(client, funcname) ret = func(*args, **kwargs) except Exception as e: _call_log(ret, str(e)) log.error(traceback.format_exc()) if self.raise_except: raise else: _call_log(ret) return ret
def remote_execute(self, func_name, **params): """ execute remote function with given func_name and params :param func_name: remote function name :param params: params :return: executed result """ if self.check_func(func_name): try: with client_context(self.agent_thrift.AgentService, self.host, self.port, socket_timeout=SOCKET_TIMEOUT, connect_timeout=CONNECT_TIMEOUT) as c: result = getattr(c, func_name)(**params) try: return json.loads(result) except Exception as e: return result except Exception as e: return {'status': "down", "message": e}
def open(self): start_time = time.time() err = '' self.server = self.server_selector.next() if not self.server: restore(self.server_selector, self.service) self.server = self.server_selector.next() if not self.server: log.error('server=%s|err=no server!', self.service.__name__) raise ThriftClientError addr = self.server['server']['addr'] try: with client_context( self.service, addr[0], addr[1], timeout=self.server['server']['timeout']) as client: client.ping() except Exception as e: err = str(e) log.error(traceback.format_exc()) self.server['valid'] = False finally: end_time = time.time() tname = self.service.__name__ pos = tname.rfind('.') if pos > 0: tname = tname[pos + 1:] s = 'server=%s|func=open|addr=%s:%d/%d|time=%d' %\ (tname, addr[0], addr[1], self.server['server']['timeout'], int((end_time - start_time) * 1000)) if err: s += '|err=%s' % repr(err) log.info(s) if not err: return 0 return -1
def restore(selector, service): invalid = selector.not_valid() log.debug('invalid server:%s', invalid) for server in invalid: transport = None try: log.debug('try restore %s', server['server']['addr']) addr = server['server']['addr'] with client_context(service, addr[0], addr[1], timeout=server['server']['timeout']) as client: client.ping() except: log.error(traceback.format_exc()) log.debug("restore fail: %s", server['server']['addr']) continue finally: if transport: transport.close() log.debug('restore ok %s', server['server']['addr']) server['valid'] = True
def identify_servers(servers): """ Gets the identifier information about each server in the given `servers` dictionary, including its ID and stream urls and returns it as a list of dicts. :param servers: a list of IPs where servers are known to exist :return: a list of dictionaries containing {server_id: {ip: ip, id: id:, stream_url: stream_url} """ out_servers = [] for ip in servers: try: with client_context(pepi_thrift.CameraServer, ip, 6000, socket_timeout=2500) as c: stream_urls = c.stream_urls() # TODO: add multi-camera support for more than 1 stream per server stream_url = stream_urls[0] if stream_urls else [] server_dict = {'ip': ip, 'id': c.identify(), 'stream_url': stream_url} out_servers.append(server_dict) except thriftpy.transport.TTransportException as e: logging.error(e) except Exception as e: logging.error(e) return out_servers
def make_request(self, thrift_request): thriftpy_service = getattr( self._thrifts[thrift_request.thrift_file], thrift_request.service_name ) thrift_spec = self.service_specs[thrift_request.thrift_file] endpoint_spec = thrift_spec[thrift_request.service_name].endpoints[ thrift_request.endpoint_name ] time_before_client = datetime.datetime.now() try: with thriftpy_client.client_context( service=thriftpy_service, host=thrift_request.host, port=thrift_request.port, proto_factory=_find_protocol_factory(thrift_request.protocol), trans_factory=_find_transport_factory(thrift_request.transport), ) as client: time_after_client = datetime.datetime.now() - time_before_client return _make_client_call( client, time_after_client, thrift_request, thriftpy_service, endpoint_spec, ) except TException as exception: status = "ConnectionError" response = "Failed to make client connection: {}".format( getattr(exception, "message") ) return ThriftResponse( status=status, request=thrift_request, data=response, time_to_make_request=None, time_to_connect=None, )
def device_online(device_id): ret = SERVICES_ONLINE if len(ret) < 1: # pass raise Exception("没有可用的服务:%s" % SERVICES_ONLINE) # 随机方式实现负载均衡 server = ret[randint(0, len(ret) - 1)].split(":") # server_addr = server[0] # server_port = int(server[1]) server_addr = "s74.53iq.com" server_port = 48076 try: with client_context(THRIFT_ONLINE.Device, server_addr, server_port, timeout=None, socket_timeout=20000) as client: r = client.get_status(device_id) print('r',r) if r.get('data') == 'online': return 1 else: return 0 except Exception as e: logging.exception(e) return None
def download_images(all_servers): """ Downloads all known images from servers in `all_servers` based on what the client has stored into a timestamped folder under '/images'. :param all_servers: a list of dictionaries of servers, with each being {id: id, ip: ip} :return: None """ image_dir = get_image_dir() downloaded_images = collections.defaultdict(list) server_data = app.server_data.copy() for id_ in server_data: server = find_server_by(all_servers, id_=id_) if not server: downloaded_images[id_].extend(server_data[id_]) continue with client_context(pepi_thrift.CameraServer, server['ip'], 6000, socket_timeout=10000) as c: for data_code in server_data[id_]: try: images = c.retrieve_stills_jpg(data_code) except ImageUnavailable as e: logging.warn(e) downloaded_images[id_].append(data_code) else: downloaded_images[id_].append(data_code) for count, image in enumerate(images): logging.info('Received data_code {}. Image length: {} bytes'.format(data_code, len(image))) out_file = open('{}/id{}_d{}_cam{}.jpeg'.format(image_dir, server['id'], data_code, count), 'wb') out_file.write(image) out_file.close() for id_, data_code_list in iteritems(downloaded_images): [server_data[id_].remove(data_code) for data_code in data_code_list] app.server_data = server_data logging.info('Client expected data codes now: {}'.format(app.server_data))
def main(): with client_context(hanlprpc_thrift.Segmenter, '127.0.0.1', 6681) as client: result = client.seg("中华人民共和国成立五十周年") print(result)
def client(timeout=3000): return client_context(addressbook.AddressBookService, unix_socket="./thriftpy_test.sock", timeout=timeout)
def test_client_connect_timeout(): with pytest.raises(TTransportException): with client_context(addressbook.AddressBookService, unix_socket='/tmp/test.sock', connect_timeout=1000) as c: c.hello('test')
def test_client_socket_timeout(): with pytest.raises(socket.timeout): with client_context(addressbook.AddressBookService, unix_socket=unix_sock, socket_timeout=500) as c: c.sleep(1000)
def client(): return client_context(addressbook.AddressBookService, '127.0.0.1', 8000)
def jvs_client(): return client_context( jvs_thrift.JvsService, config.JVS_THRIFT_SERVICE['host'], config.JVS_THRIFT_SERVICE['port'] )
# coding=utf-8 import os import thriftpy from thriftpy.rpc import client_context from thriftpy.protocol import TBinaryProtocolFactory from thriftpy.transport import TBufferedTransportFactory HERE = os.path.abspath(os.path.dirname(__file__)) calc_thrift = thriftpy.load( os.path.join(HERE, 'calc.thrift'), module_name='calc_thrift') with client_context(calc_thrift.CalcService, '127.0.0.1', 8300, proto_factory=TBinaryProtocolFactory(), trans_factory=TBufferedTransportFactory(), timeout=None) as calc: rs = calc.add(1, 2) print 'Result is: {}'.format(rs)
def request(self): host,port=("localhost", 8000) with client_context(goods.GoodsService, host, port) as c: response = c.getCategories(3724) print(response)
def client(timeout=3000): return client_context(addressbook.AddressBookService, unix_socket=unix_sock, timeout=timeout)
def main(): with client_context(pingpong.PingService, '127.0.0.1', 6000) as c: pong = c.ping() print(pong)
def client(timeout=None): return client_context(addressbook.AddressBookService, '127.0.0.1', 8000, timeout=timeout)
def client(self): return client_context(addressbook.AddressBookService, host="localhost", port=self.PORT, proto_factory=self.PROTOCOL_FACTORY, trans_factory=self.TRANSPORT_FACTORY)