def __init__(self, processor, lsocket, inputProtocolFactory=None, outputProtocolFactory=None, threads=10): self.processor = processor self.socket = lsocket self.in_protocol = inputProtocolFactory or TBinaryProtocolFactory() self.out_protocol = outputProtocolFactory or self.in_protocol self.threads = int(threads) self.clients = {} self.tasks = Queue.Queue() self._read, self._write = socket.socketpair() self.prepared = False self._stop = False
def get_protocol_factory(protocol): """ Returns a protocol factory associated with the string protocol passed in as a command line argument to the cross runner :param protocol: string :return: Protocol factory """ if protocol == "binary": return FProtocolFactory(TBinaryProtocolFactory()) elif protocol == "compact": return FProtocolFactory(TCompactProtocolFactory()) elif protocol == "json": return FProtocolFactory(TJSONProtocolFactory()) else: logging.error("Unknown protocol type: %s", protocol) sys.exit(1)
def __init__(self, processor, lsocket, inputProtocolFactory=None, outputProtocolFactory=None, threads=0): assert threads == 0 # Modified thrift server implementation self.processor = processor self.socket = lsocket self.in_protocol = inputProtocolFactory or TBinaryProtocolFactory() self.out_protocol = outputProtocolFactory or self.in_protocol self.clients = {} self.tasks = Queue.Queue() self._read, self._write = socket.socketpair() self._accept_socket = socket.socket() self.prepared = False self._stop = False
def test(): try: transport = TStreamPool('127.0.0.1', 9095, max_stream=10) client = PoolClient(Client, transport, TBinaryProtocolFactory()) for i in range(0, 20): res = yield client.translate( 'Die USA und Großbritannien berichten von einer mutmaßlichen weltweiten Cyberattacke.' ) res = res.replace('@@ ', '') print(res) res = yield client.translate( 'Von der Regierung in Moskau unterstützte Hacker-Gruppen hätten Router, Switches und Firewalls infiziert, so Behörden beider Länder.' ) res = res.replace('@@ ', '') print(res) except Thrift.TException as ex: print("%s" % ex.message) ioloop.stop()
def test_process(self): processor = FBaseProcessor() proc = Mock() future = Future() future.set_result(None) proc.process.return_value = future processor.add_to_processor_map("basePing", proc) frame = bytearray( b'\x00\x00\x00\x00\x0e\x00\x00\x00\x05_opid\x00\x00\x00\x011' b'\x80\x01\x00\x02\x00\x00\x00\x08basePing\x00\x00\x00\x00\x00') itrans = TMemoryBuffer(value=frame) iprot = FProtocolFactory(TBinaryProtocolFactory()).get_protocol(itrans) oprot = Mock() yield processor.process(iprot, oprot) assert (proc.process.call_args) args, _ = proc.process.call_args assert (args[0]._get_op_id() == 1) assert (args[1] == iprot) assert (args[2] == oprot)
async def test_process(self): processor = FBaseProcessor() proc = Mock() future = Future() future.set_result(None) proc.process.return_value = future processor.add_to_processor_map("basePing", proc) frame = bytearray( b'\x00\x00\x00\x00\x0e\x00\x00\x00\x05_opid\x00\x00\x00\x011' b'\x80\x01\x00\x02\x00\x00\x00\x08basePing\x00\x00\x00\x00\x00') itrans = TMemoryBuffer(value=frame) iprot = FProtocolFactory(TBinaryProtocolFactory()).get_protocol(itrans) oprot = Mock() await processor.process(iprot, oprot) assert (proc.process.call_args) args, _ = proc.process.call_args self.assertEqual(args[0].get_response_header(_OPID_HEADER), '1') assert (args[1] == iprot) assert (args[2] == oprot)
def __init__(self, host=None, port=10000, authMechanism=None, user=None, password=None, configuration=None): super(TornadoConnection, self).__init__(authMechanism) #Must set a password for thrift, even if it doesn't need one #Open issue with python-sasl password = self._check_password(authMechanism, password) if authMechanism == "NOSASL": self.transport = TTornadoStreamTransport(host, port) else: saslc, sasl_mech = self._get_sasl_client(host, authMechanism, user, password, configuration) self.transport = TSaslClientTransportTornado( saslc, sasl_mech, host, port) pfactory = TBinaryProtocolFactory() self.client = TCLIServiceTornado.Client(self.transport, pfactory)
def init_pool(self): return PoolClient(Client, TStreamPool(self.host, self.port, max_stream=self.max_stream), TBinaryProtocolFactory())
from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals import unittest from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.protocol.TBinaryProtocol import TBinaryProtocolFactory from thrift.util.Serializer import serialize, deserialize from Recursive.ttypes import * fac = TBinaryProtocolFactory() class TestRecursivePythonStructs(unittest.TestCase): def test_tree(self): tree = RecTree() child = RecTree() tree.children = [child] ser = serialize(fac, tree) result = RecTree() result = deserialize(fac, ser, result) self.assertEqual(result, tree) def test_list(self): l = RecList() l2 = RecList() l.next = l2 ser = serialize(fac, l)
def main(): parser = argparse.ArgumentParser(description="Run a tornado python server") parser.add_argument('--port', dest='port', default='9090') parser.add_argument('--protocol', dest='protocol_type', default="binary", choices="binary, compact, json") parser.add_argument('--transport', dest="transport_type", default="stateless", choices="stateless, http") args = parser.parse_args() if args.protocol_type == "binary": protocol_factory = FProtocolFactory(TBinaryProtocolFactory()) elif args.protocol_type == "compact": protocol_factory = FProtocolFactory(TCompactProtocolFactory()) elif args.protocol_type == "json": protocol_factory = FProtocolFactory(TJSONProtocolFactory()) else: logging.error("Unknown protocol type: %s", args.protocol_type) sys.exit(1) nats_client = NATS() options = {"verbose": True, "servers": ["nats://127.0.0.1:4222"]} yield nats_client.connect(**options) global port port = args.port handler = FrugalTestHandler() subject = "frugal.*.*.rpc.{}".format(args.port) processor = Processor(handler) if args.transport_type == "stateless": server = FNatsServer(nats_client, [subject], processor, protocol_factory) # start healthcheck so the test runner knows the server is running thread.start_new_thread(healthcheck, (port, )) print("Starting {} server...".format(args.transport_type)) yield server.serve() elif args.transport_type == "http": factories = { 'processor': processor, 'protocol_factory': protocol_factory } server = Application([(r'/', FHttpHandler, factories)]) print("Starting {} server...".format(args.transport_type)) server.listen(port) else: logging.error("Unknown transport type: %s", args.transport_type) sys.exit(1) # Setup subscriber, send response upon receipt pub_transport_factory = FNatsPublisherTransportFactory(nats_client) sub_transport_factory = FNatsSubscriberTransportFactory(nats_client) provider = FScopeProvider(pub_transport_factory, sub_transport_factory, protocol_factory) global publisher publisher = EventsPublisher(provider) yield publisher.open() @gen.coroutine def response_handler(context, event): print("received {} : {}".format(context, event)) preamble = context.get_request_header(PREAMBLE_HEADER) if preamble is None or preamble == "": logging.error("Client did not provide preamble header") return ramble = context.get_request_header(RAMBLE_HEADER) if ramble is None or ramble == "": logging.error("Client did not provide ramble header") return response_event = Event(Message="Sending Response") response_context = FContext("Call") global publisher global port yield publisher.publish_EventCreated(response_context, preamble, ramble, "response", "{}".format(port), response_event) print("Published event={}".format(response_event)) subscriber = EventsSubscriber(provider) yield subscriber.subscribe_EventCreated("*", "*", "call", "{}".format(args.port), response_handler)
def run(self): processor = ThriftProcessor(self._interface) server = self._server_type(processor, self._transport, TFramedTransportFactory(), TBinaryProtocolFactory()) return server.serve()
def setUp(self): self.fac = TBinaryProtocolFactory()
def get_client(self, host, port, max_connections = 0): key = "%s:%s" % (host, port) if key not in self.client_pools: self.__class__.client_pools[key] = PoolClient(Client, TStreamPool(host, port, max_stream=max_connections), TBinaryProtocolFactory()) elif max_connections: self.client_pools[key]._itrans_pool._max_stream = max_connections return self.client_pools[key]
def test_TBinaryProtocol(self): buf = TTransport.TMemoryBuffer() transport = TTransport.TBufferedTransportFactory().getTransport(buf) factory = TBinaryProtocolFactory(transport) self.verify(self.binary_serialized, factory)