def setup(): # Make socket transport = TSocket.TSocket('nlp.rit.edu', 9010) # Buffering is critical. Raw sockets are very slow transport = TTransport.TFramedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Curator.Client(protocol) return transport, client
def __iter__(self): if self._storage_id is None or self._qbeast_id is None: ''' In this case, we are doing a query without splitting the object, and thus we have to initialize the query for only this iterator ''' if type(config.qbeast_entry_node) == list: qbeast_node = config.qbeast_entry_node[0] else: qbeast_node = config.qbeast_entry_node transport = TSocket.TSocket(qbeast_node, config.qbeast_master_port) # Buffering is critical. Raw sockets are very slow transport = TTransport.TFramedTransport(transport) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = QbeastMaster.Client(protocol) # Connect! transport.open() area = FilteringArea(fromPoint=self._qbeast_meta.from_point, toPoint=self._qbeast_meta.to_point) if self._storage_id is None: self._storage_id = uuid.uuid4() self._build_args = self._build_args._replace( storage_id=self._storage_id) self._store_meta(self._build_args) uuids = [str(self._storage_id)] log.info( "calling initQuery (%s, %s, %s, precision=%f ,area=%s, uuids=%s, max_results=%f", self._selects, self._ksp, self._table, self._qbeast_meta.precision, area, uuids, config.qbeast_max_results) self._qbeast_id = client.initQuery( self._selects, self._ksp + '_qbeast', self._table + '_' + self._table + '_idx_d8tree', area, self._qbeast_meta.precision, config.qbeast_max_results, uuids) self._store_meta(self._build_args) transport.close() return IndexedIterValue(self._storage_id, self._entry_point, self._row_builder)
def agent_conn_open(): import socket import sys from thrift.transport import TTransport, TSocket from thrift.protocol import TBinaryProtocol, TProtocol from thrift.transport.TTransport import TTransportException try: socket = TSocket.TSocket("localhost", 9090) transport = TTransport.TFramedTransport(socket) transport.open() protocol = TBinaryProtocol.TBinaryProtocol(transport) return protocol except TTransportException: return None
def read_reviews(): socket = TSocket.TSocket("ath-8.ece.cornell.edu", 10007) transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = ReviewStorageService.Client(protocol) transport.open() for i in range(100): req_id = random.randint(0, 99) review_ids = set() for j in range(10): review_ids.add(random.randint(0, 99)) print(client.ReadReviews(req_id, review_ids, {})) transport.close()
def _get_service(): hostport_info = _get_backend_host() if not hostport_info: warnings.warn( "Unable to record error: flawless server hostport not set", RuntimeWarning) return None, None, None tsocket = TSocket.TSocket(hostport_info.host, hostport_info.port) tsocket.setTimeout(2000) # 2 second timeout transport = TTransport.TFramedTransport(tsocket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Flawless.Client(protocol) return client, transport, hostport_info
def connect_to_service(self, path_serv, ttype="framed"): """ Устанавливаем соединение с сервисом по протоколу Thrift. :param ttype: тип транспорта бинарного протокола :return: <type client> """ turl = configs.config["env_info"][prefix_serv + "_url"] tpath = configs.config["backend"][path_serv] thost = turl if path_serv == 'root' else turl + tpath tport = configs.config["env_info"][prefix_serv + "_port"] try: # Если соединение уже было установленно, используем его #if 'thrift_transport' not in globals(): # global thrift_transport thrift_transport = None socket = TSocket.TSocket(thost, tport) service_log.put("Make socket: %s" % socket) if ttype == "tframed": thrift_transport = TTransport.TFramedTransport(socket) service_log.put("Create TFramedTransport: %s" % thrift_transport) elif ttype == "tbuff": # Делаем буферизацию. Работа с сокетами очень медленная. thrift_transport = TTransport.TBufferedTransport(socket) service_log.put("Create TBufferedTransport: %s" % thrift_transport) service_log.put( "Buffering is critical. Raw sockets are very slow.") else: error_message = "Is not a valid binary protocol type" service_log.put(error_message) raise AssertionError(error_message) # Делаем врапер для протокола protocol = TBinaryProtocol.TBinaryProtocol(thrift_transport) service_log.put("Wrap in a protocol.") # Создаём клиента для работы с протоклом декодирования client = self.worker.Client(protocol) service_log.put("Create a client to use the protocol encoder.") # Коннектимся thrift_transport.open() service_log.put("Transport - open. Connect!") service_log.put("Control is returned to the method to call.") return client except Thrift.TException, tx: service_log.error(tx.message)
def demo_client(host, port, is_framed_transport): # Make socket socket = TSocket.TSocket(host, port) # Make transport if is_framed_transport: transport = TTransport.TFramedTransport(socket) else: transport = TTransport.TBufferedTransport(socket) # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder client = Client(protocol) # Connect! transport.open() t = "demo_table" # # Scan all tables, look for the demo table and delete it. # print "scanning tables..." for table in client.getTableNames(): print " found: %s" % (table) if table == t: if client.isTableEnabled(table): print " disabling table: %s" % (t) client.disableTable(table) print " deleting table: %s" % (t) client.deleteTable(table) columns = [] col = ColumnDescriptor() col.name = 'entry:' col.maxVersions = 10 columns.append(col) col = ColumnDescriptor() col.name = 'unused:' columns.append(col) try: print "creating table: %s" % (t) client.createTable(t, columns) except AlreadyExists, ae: print "WARN: " + ae.message
def _open(self): """Open the IO stream. Make it ready to output thrift data to local disk. """ try: # Firstly, make sure close all IO stream if self._protocol: LOG.warning( "Thrift protocol already exists! Need to close and reopen it." ) self._protocol = None if self._transport: LOG.warning( "Thrift transport already exists! Need to close and reopen it." ) self._transport.close() self._transport = None # self._file_stream should be closed by self._transport.close() self._file_stream = None except IOError as ioe: util.error_text_logging( LOG, 'ThriftLogger for topic %s failed to open the IO stream!\n ' 'I/O error (%s) : %s' % (self._topic, ioe.errno, ioe.strerror)) except: util.error_text_logging( LOG, 'ThriftLogger for topic %s failed to open the IO stream due to ' 'unexpected error:\n %s' % (self._topic, sys.exc_info()[0])) try: # Secondly, open new IO stream self._file_stream = open(self._working_file_name, 'ab') self._transport = TTransport.TFramedTransport( TTransport.TFileObjectTransport(self._file_stream)) self._protocol = TBinaryProtocol.TBinaryProtocol(self._transport) self._transport.open() except IOError as ioe: util.error_text_logging( LOG, 'ThriftLogger for topic %s failed to open the IO stream!\n ' 'I/O error (%s) : %s' % (self._topic, ioe.errno, ioe.strerror)) except: util.error_text_logging( LOG, 'ThriftLogger for topic %s failed to open the IO stream due to ' 'unexpected error:\n %s' % (self._topic, sys.exc_info()[0]))
def _create_client(self, address): host, port = address sock = TSocket.TSocket(host, port) sock.setTimeout(self._client_timeout) transport = TTransport.TFramedTransport(sock) protocol = TCompactProtocol.TCompactProtocol(transport) mp = TMultiplexedProtocol(protocol, self._service_name) client = self._client_class(mp) self._transports[client] = transport self._sockets[client] = sock transport.open() return client
def read_user_reviews(): socket = TSocket.TSocket("ath-8.ece.cornell.edu", 9090) transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = UserReviewService.Client(protocol) transport.open() for i in range(100): req_id = random.getrandbits(63) user_id = random.randint(0, 5) start = random.randint(0, 10) stop = start + random.randint(1, 10) print(client.ReadUserReviews(req_id, user_id, start, stop, {})) transport.close()
def wrtie_cast_info(): socket = TSocket.TSocket("ath-8.ece.cornell.edu", 10010) transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = CastInfoService.Client(protocol) transport.open() for i in range(100): req_id = random.getrandbits(63) cast_id = i name = "name_" + str(i) gender = random.randint(0, 1) intro = "intro_" + str(i) client.WriteCastInfo(req_id, cast_id, name, gender, intro, {}) transport.close()
def open(self, set_keyspace=False, login=False): if self.transport == None: # Create the client connection to the Cassandra daemon socket = TSocket.TSocket(self.host, int(self.port)) transport = TTransport.TFramedTransport(TTransport.TBufferedTransport(socket)) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) transport.open() self.transport = transport self.client = Cassandra.Client(protocol) if login: self.login() if set_keyspace: self.set_keyspace()
def write_movie_review(): socket = TSocket.TSocket("ath-8.ece.cornell.edu", 9090) transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = MovieReviewService.Client(protocol) transport.open() for i in range(101, 200): req_id = random.getrandbits(63) timestamp = int(time() * 1000) movie_num = random.randint(0, 5) movie_id = "movie_id_" + str(movie_num) client.UploadMovieReview(req_id, movie_id, i, timestamp, {}) transport.close()
def Init(self): try: socket = TSocket.TSocket(self.host, self.port) transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) protocol_multi = TMultiplexedProtocol(protocol, "Feature Service") client = FeatureService.Client(protocol_multi) transport.open() self.client = client #print("connect success") return True except Exception, e: print("InitClient exception:%s" % e) return False
def run(self): category_name = getLocalIp() + "%" + self.handleFile.split( '/')[-2] + "%" + self.handleFile.split('/')[-1] socket = TSocket.TSocket(host=self.host, port=self.port) transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(trans=transport, strictRead=False, strictWrite=False) client = scribe.Client(iprot=protocol, oprot=protocol) try: transport.open() except Exception, e: syslog.syslog("%s\n" % e) sys.exit(2)
def _open_connection(self, address): """ Opens a connection with a server address. :param address: the address of the server to connect to """ (url, port) = self._parse_address_for_hostname_and_port(address) if self._proxy: proxy_host, proxy_port = self._proxy.split(":") self._transport = TProxySocket(proxy_host, proxy_port, url, port) else: self._transport = TSocket.TSocket(url, port) self._transport = TTransport.TFramedTransport(self._transport) self._transport.open() self._protocol = TFinagleProtocol(self._transport, client_id=self._client_id)
def setUp(self): if self.ssl: self.socket = TSSLSocket.TSSLSocket("localhost", self._port) else: self.socket = TSocket.TSocket("localhost", self._port) if self.server_type in FRAMED_TYPES \ and not isinstance(self, HeaderTest): self.transport = TTransport.TFramedTransport(self.socket) else: self.transport = TTransport.TBufferedTransport(self.socket) self.protocol = self.protocol_factory.getProtocol(self.transport) self.transport.open() self.client = ThriftTest.Client(self.protocol)
def signalFrontEnd(self): sock = TSocket.TSocket(host=self.hostFE, port=self.portFE) transport = TTransport.TFramedTransport(sock) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = ClientFE(protocol) transport.open() print( f"[Back-end] Pinging Front-end with {self.hostFE}: {self.portBE}") result = client.registerNode(self.hostFE, self.portBE) if result: print("Succesful") else: print("Already in List") sock.close() transport.close()
def __init__(self, host="localhost", port=50096, user='******', password='******', _connect=True): super(Accumulo, self).__init__() self.transport = TTransport.TFramedTransport( TSocket.TSocket(host, port)) self.protocol = TCompactProtocol.TCompactProtocol(self.transport) self.client = AccumuloProxy.Client(self.protocol) if _connect: self.transport.open() self.login = self.client.login(user, {'password': password})
def connect(self): if self.transport: self.close() self._before_reconnect() self.transport = TSocket.TSocket(self.host, self.port) # Buffering is critical. Raw sockets are very slow if self.timeout > 0: self.transport.setTimeout(self.timeout) self.transport = TTransport.TFramedTransport(self.transport) # Wrap in a protocol protocol = TCompactProtocol.TCompactProtocol(self.transport) # Create a client to use the protocol encoder self.client = self.service.Client(protocol) # Connect! self.transport.open()
def __init__(self, settings): self.DB_URI = settings['HBASE_URI'] self.DB_PORT = settings['HBASE_PORT'] self.TB_INFO = settings['TB_INFO'].encode() self.TB_NEWS = settings['TB_NEWS'].encode() # 连接数据库表 socket = TSocket.TSocket(self.DB_URI, self.DB_PORT) self.transport = TTransport.TFramedTransport(socket) protocol = TCompactProtocol.TCompactProtocol(self.transport) self.client = THBaseService.Client(protocol) self.transport.open() # 将爬虫开始的信息存入数据库 self.spider_info_row_key, start_put = gen_start_spider_info() self.client.put(self.TB_INFO, start_put)
def setUp(self): global options socket = TSocket.TSocket(options.host, options.port) # Frame or buffer depending upon args if options.framed_input or options.framed_output: self.transport = TTransport.TFramedTransport( socket, options.framed_input, options.framed_output) else: self.transport = TTransport.TBufferedTransport(socket) self.transport.open() protocol = self.protocol_factory.getProtocol(self.transport) self.client = ThriftTest.Client(protocol)
def createConnection(): socket = TSocket.TSocket(host, port) if framed: transport = TTransport.TFramedTransport(socket) else: if secureMode: transport = TTransport.TSaslClientTransport( socket, host, saslServiceName) else: transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = THBaseService.Client(protocol) transport.open() return client, transport
def main(): transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TFramedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Calculator.Client(protocol) transport.open() client.ping() sum_ = client.add(1, 1) print("1+1=%d" % sum_) client.sub(2, 1) transport.close()
def create_learn(addr='localhost', port=LEARN_PORT): learn = {} # Make socket socket = TSocket.TSocket(addr, port) # Buffering is critical. Raw sockets are very slow learn['transport'] = TTransport.TFramedTransport(socket) learn['transport'].open() # Wrap in a protocol protocol = TBinaryProtocol.TBinaryProtocol(learn['transport']) # Create a client to use the protocol encoder learn['client'] = ParserService.Client(protocol) return learn
def process(self, req): try: socket = TSocket.TSocket(self.host, self.port) #socket = TSocket.TSocket('192.168.35.37', 5555) #socket = TSocket.TSocket('10.39.18.58', 5100) transport = TTransport.TFramedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) # protocol = TCompactProtocol.TCompactProtocol(transport) client = Client(protocol) transport.open() res = client.SearchInner(req) transport.close() return res except Thrift.TException, e: print e.message
def main(): # 以frame为单位进行传输,非阻塞式服务中使用 transport = TTransport.TFramedTransport( TSocket.TSocket(host="127.0.0.1", port=9999)) transport.open() # 序列化方式 protocol = TBinaryProtocol.TBinaryProtocol(transport) multiplexedprotocol = TMultiplexedProtocol(protocol, "calService") client = Calculator.Client(multiplexedprotocol) param = CalParam(num1=1, num2=2, op=Operation.ADD) re = client.getResult(param) print(param) print(re) transport.close()
def login(host, port, name, password, appId, appGroupId): from thrift.transport import TTransport from thrift.transport import TSocket from thrift.protocol import TCompactProtocol from aclSystem import UserService try: logsocket = TSocket.TSocket(host, port) transport = TTransport.TFramedTransport(logsocket) protocol = TCompactProtocol.TCompactProtocol(transport) transport.open() global loginClient loginClient = UserService.Client(protocol) return loginClient.login(name, password, appId, appGroupId) except Exception: return None
def _build_server(self, class_, host, port, **conn_args): """Return a client for the given host and port.""" try: socket_ = TSocket.TSocket(host, int(port)) if self._timeout: socket_.setTimeout(self._timeout) transport = TTransport.TFramedTransport(socket_) protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) client = class_(protocol, **conn_args) client.transport = transport setattr(client, 'host', host) setattr(client, 'port', port) return client except (Thrift.TException, cas_types.InvalidRequestException, cas_types.UnavailableException): return None
def _rpc_client_instance(self): """ Return an RPC client connection """ import thrift.transport.TSocket as TSocket socket = TSocket.TSocket('localhost', self._server_port) try: transport = TTransport.TFramedTransport(socket) transport.open() except TTransport.TTransportException: logging.error('Unable to connect to contrail-vrouter-agent ' 'localhost:%d' % self._server_port) return None protocol = TBinaryProtocol.TBinaryProtocol(transport) client = InstanceService.Client(protocol) if client and self._connect: client.Connect() return client