Exemple #1
0
def main(cfg):
    if cfg.unix:
        if cfg.addr == "":
            sys.exit("invalid listener unix domain socket: {}".format(cfg.addr))
    else:
        try:
            (host, port) = cfg.addr.rsplit(":", 1)
            port = int(port)
        except ValueError:
            sys.exit("invalid listener address: {}".format(cfg.addr))

    if cfg.response == "success":
        handler = SuccessHandler()
    elif cfg.response == "idl-exception":
        handler = IDLExceptionHandler()
    elif cfg.response == "exception":
        # squelch traceback for the exception we throw
        logging.getLogger().setLevel(logging.CRITICAL)
        handler = ExceptionHandler()
    else:
        sys.exit("unknown server response mode {0}".format(cfg.response))

    processor = Example.Processor(handler)
    if cfg.service is not None:
        # wrap processor with multiplexor
        multi = TMultiplexedProcessor.TMultiplexedProcessor()
        multi.registerProcessor(cfg.service, processor)
        processor = multi

    if cfg.unix:
        transport = TSocket.TServerSocket(unix_socket=cfg.addr)
    else:
        transport = TSocket.TServerSocket(host=host, port=port)

    if cfg.transport == "framed":
        transport_factory = TTransport.TFramedTransportFactory()
    elif cfg.transport == "unframed":
        transport_factory = TTransport.TBufferedTransportFactory()
    elif cfg.transport == "header":
        if cfg.protocol == "binary":
            transport_factory = THeaderTransport.THeaderTransportFactory(
                THeaderTransport.T_BINARY_PROTOCOL)
        elif cfg.protocol == "compact":
            transport_factory = THeaderTransport.THeaderTransportFactory(
                THeaderTransport.T_COMPACT_PROTOCOL)
        else:
            sys.exit("header transport cannot be used with protocol {0}".format(cfg.protocol))
    else:
        sys.exit("unknown transport {0}".format(cfg.transport))

    if cfg.protocol == "binary":
        protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
    elif cfg.protocol == "compact":
        protocol_factory = TCompactProtocol.TCompactProtocolFactory()
    elif cfg.protocol == "json":
        protocol_factory = TJSONProtocol.TJSONProtocolFactory()
    else:
        sys.exit("unknown protocol {0}".format(cfg.protocol))

    print(
        "Thrift Server listening on {0} for {1} {2} requests".format(
            cfg.addr, cfg.transport, cfg.protocol))
    if cfg.service is not None:
        print("Thrift Server service name {0}".format(cfg.service))
    if cfg.response == "idl-exception":
        print("Thrift Server will throw IDL exceptions when defined")
    elif cfg.response == "exception":
        print("Thrift Server will throw Thrift exceptions for all messages")

    server = TServer.TThreadedServer(processor, transport, transport_factory, protocol_factory)
    try:
        server.serve()
    except KeyboardInterrupt:
        print
Exemple #2
0
def main(cfg, reqhandle, resphandle):
    if cfg.unix:
        if cfg.addr == "":
            sys.exit("invalid unix domain socket: {}".format(cfg.addr))
        socket = TSocket.TSocket(unix_socket=cfg.addr)
    else:
        try:
            (host, port) = cfg.addr.rsplit(":", 1)
            if host == "":
                host = "localhost"
            socket = TSocket.TSocket(host=host, port=int(port))
        except ValueError:
            sys.exit("invalid address: {}".format(cfg.addr))

    transport = TRecordingTransport(socket, reqhandle, resphandle)

    if cfg.transport == "framed":
        transport = TTransport.TFramedTransport(transport)
    elif cfg.transport == "unframed":
        transport = TTransport.TBufferedTransport(transport)
    elif cfg.transport == "header":
        transport = THeaderTransport.THeaderTransport(
            transport,
            client_type=THeaderTransport.CLIENT_TYPE.HEADER,
        )
    else:
        sys.exit("unknown transport {0}".format(cfg.transport))

    transport.open()

    if cfg.protocol == "binary":
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
    elif cfg.protocol == "compact":
        protocol = TCompactProtocol.TCompactProtocol(transport)
    elif cfg.protocol == "json":
        protocol = TJSONProtocol.TJSONProtocol(transport)
    elif cfg.protocol == "finagle":
        protocol = TFinagleProtocol(transport, client_id="thrift-playground")
    else:
        sys.exit("unknown protocol {0}".format(cfg.protocol))

    if cfg.service is not None:
        protocol = TMultiplexedProtocol.TMultiplexedProtocol(
            protocol, cfg.service)

    client = Example.Client(protocol)

    try:
        if cfg.method == "ping":
            client.ping()
            print("client: pinged")
        elif cfg.method == "poke":
            client.poke()
            print("client: poked")
        elif cfg.method == "add":
            if len(cfg.params) != 2:
                sys.exit("add takes 2 arguments, got: {0}".format(cfg.params))

            a = int(cfg.params[0])
            b = int(cfg.params[1])
            v = client.add(a, b)
            print("client: added {0} + {1} = {2}".format(a, b, v))
        elif cfg.method == "execute":
            param = Param(return_fields=cfg.params,
                          the_works=TheWorks(
                              field_1=True,
                              field_2=0x7f,
                              field_3=0x7fff,
                              field_4=0x7fffffff,
                              field_5=0x7fffffffffffffff,
                              field_6=-1.5,
                              field_7=u"string is UTF-8: \U0001f60e",
                              field_8=b"binary is bytes: \x80\x7f\x00\x01",
                              field_9={
                                  1: "one",
                                  2: "two",
                                  3: "three"
                              },
                              field_10=[1, 2, 4, 8],
                              field_11=set(["a", "b", "c"]),
                              field_12=False,
                          ))

            try:
                result = client.execute(param)
                print("client: executed {0}: {1}".format(param, result))
            except AppException as e:
                print("client: execute failed with IDL Exception: {0}".format(
                    e.why))
        else:
            sys.exit("unknown method {0}".format(cfg.method))
    except Thrift.TApplicationException as e:
        print("client exception: {0}: {1}".format(e.type, e.message))

    if cfg.request is None:
        req = "".join(["%02X " % ord(x) for x in reqhandle.getvalue()]).strip()
        print("request: {}".format(req))
    if cfg.response is None:
        resp = "".join(["%02X " % ord(x)
                        for x in resphandle.getvalue()]).strip()
        print("response: {}".format(resp))

    transport.close()