예제 #1
0
def server(request):
    p1 = TProcessor(mux.ThingOneService, DispatcherOne())
    p2 = TProcessor(mux.ThingTwoService, DispatcherTwo())

    mux_proc = TMultiplexedProcessor()
    mux_proc.register_processor("ThingOneService", p1)
    mux_proc.register_processor("ThingTwoService", p2)

    _server = TThreadedServer(mux_proc,
                              TServerSocket(unix_socket=sock_path),
                              iprot_factory=TBinaryProtocolFactory(),
                              itrans_factory=TBufferedTransportFactory())
    ps = multiprocessing.Process(target=_server.serve)
    ps.start()
    time.sleep(0.1)

    def fin():
        if ps.is_alive():
            ps.terminate()
        try:
            os.remove(sock_path)
        except IOError:
            pass

    request.addfinalizer(fin)
예제 #2
0
def main():
    dd_proc = TProcessor(dd_thrift.DingService, DingDispatcher())
    pp_proc = TProcessor(pp_thrift.PingService, PingDispatcher())

    mux_proc = TMultiplexedProcessor()
    mux_proc.register_processor(DD_SERVICE_NAME, dd_proc)
    mux_proc.register_processor(PP_SERVICE_NAME, pp_proc)

    server = TThreadedServer(mux_proc, TServerSocket(),
                             iprot_factory=TBinaryProtocolFactory(),
                             itrans_factory=TBufferedTransportFactory())
    server.serve()
예제 #3
0
파일: rpc.py 프로젝트: xhochy/thriftpy2
def make_server(service,
                handler,
                host="localhost",
                port=9090,
                unix_socket=None,
                proto_factory=TBinaryProtocolFactory(),
                trans_factory=TBufferedTransportFactory(),
                client_timeout=3000,
                certfile=None):
    processor = TProcessor(service, handler)

    if unix_socket:
        server_socket = TServerSocket(unix_socket=unix_socket)
        if certfile:
            warnings.warn("SSL only works with host:port, not unix_socket.")
    elif host and port:
        if certfile:
            server_socket = TSSLServerSocket(host=host,
                                             port=port,
                                             client_timeout=client_timeout,
                                             certfile=certfile)
        else:
            server_socket = TServerSocket(host=host,
                                          port=port,
                                          client_timeout=client_timeout)
    else:
        raise ValueError("Either host/port or unix_socket must be provided.")

    server = TThreadedServer(processor,
                             server_socket,
                             iprot_factory=proto_factory,
                             itrans_factory=trans_factory)
    return server
예제 #4
0
    def new_server(self,
                   service,
                   responder,
                   exchange='amq.topic',
                   routing_keys=None,
                   queue=None,
                   transport_mode=TTransport_R.SERVER):

        if queue is None:
            queue = '{}.{}'.format(service.__module__, service.__name__)

        if routing_keys is True:
            routing_keys = [
                '{}.{}.{}'.format(service.__module__, service.__name__, s)
                for s in service.thrift_services
            ]

        s_transport = TTransport_R(self.rmq_client,
                                   transport_mode,
                                   amqp_exchange=exchange,
                                   amqp_queue=queue,
                                   routing_keys=routing_keys)

        processor = TProcessor(service, responder)

        server = TThreadedServer_R(processor, s_transport,
                                   TTransportFactory_R(),
                                   TBinaryProtocolFactory_R())

        server.add_close_action(s_transport.shutdown)
        self.children.append(server)
        self.servers.append(server)
        return server
예제 #5
0
def make_server(service,
                handler,
                host,
                port,
                proto_factory=TBinaryProtocolFactory()):
    processor = TProcessor(service, handler)
    server = THttpServer(processor, (host, port), iprot_factory=proto_factory)
    return server
예제 #6
0
def main() -> None:
    Utils.configure_logger("log/server.log")
    logger = logging.getLogger(__name__)

    s = schema.Schema({
        "--ipv6": bool,
        "--port": schema.Use(int),
        "--help": bool,
    })

    arguments = s.validate(docopt(__doc__))

    current_script_dir = os.path.dirname(os.path.abspath(__file__))
    thrift_path = os.path.join(current_script_dir,
                               "thrift/logging_service.thrift")
    logger.info(f"Loading the thrift definition from: {thrift_path};")
    logging_service_thrift = thriftpy2.load(
        thrift_path,
        module_name="logging_service_thrift",
    )

    if arguments["--ipv6"]:
        socket_family = socket.AF_INET6
        any_host_interface = "::"
        socket_family_name = "IPv6"
    else:
        socket_family = socket.AF_INET
        any_host_interface = "0.0.0.0"
        socket_family_name = "IPv4"

    server_port = arguments["--port"]

    queue_manager = MemoryQueueManager()
    logging_client = MetaLoggingClient()
    metadata_manager = MetadataManager(queue_manager, logging_client)
    handler = LoggingServiceHandler(metadata_manager, logging_client,
                                    logging_service_thrift)

    proc = TProcessor(logging_service_thrift.LoggingService, handler)
    server = TThreadedServer(
        proc,
        TServerSocket(
            host=any_host_interface,
            socket_family=socket_family,
            port=server_port,
            client_timeout=CLIENT_TIMEOUT_MS,
        ),
        iprot_factory=TBinaryProtocolFactory(),
        itrans_factory=TBufferedTransportFactory(),
    )

    logger.info(
        f"Logging service server listens host={any_host_interface}[{socket_family_name}], port={server_port}."
    )
    server.serve()
    logger.info("done.")
예제 #7
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import time
import thriftpy2
from thriftpy2.thrift import TProcessor

thrift_service = thriftpy2.load(
    os.path.join(os.path.dirname(__file__), "pingpong.thrift"),
    "pingpong_thrift")  # noqa
service = thrift_service.PingService


class PingpongServer(object):
    def ping(self):
        if os.environ.get('about_to_shutdown') == '1':
            raise service.AboutToShutDownException
        return "pong"

    def win(self):
        return "Yes, you win"

    def sleep(self, seconds):
        time.sleep(seconds)
        return 'good morning'


app = TProcessor(service, PingpongServer())
예제 #8
0
    def fetch_all_works(self, uid):
        return json.dumps(kol.fetch_all_video(uid))

    def checkout_user_agent(self):
        return kol.checkout_user_agent()


if sys.platform == 'win32':
    kol_thrift = thriftpy.load(os.path.join(
        PRO_DIR, '.\\dolphin\\service\\douyin\\data\\kol_thrift.thrift'),
                               module_name='kol_thrift_thrift')
else:
    kol_thrift = thriftpy.load(os.path.join(
        PRO_DIR, './dolphin/service/douyin/data/kol_thrift.thrift'),
                               module_name='kol_thrift_thrift')

app = TProcessor(kol_thrift.KolServer, KolDispatcher())
# server = TProcessor(kol_thrift.KolServer, KolDispatcher())
#
if __name__ == '__main__':
    server = make_server(kol_thrift.KolServer,
                         KolDispatcher(),
                         '0.0.0.0',
                         6000,
                         proto_factory=TCyBinaryProtocolFactory(),
                         trans_factory=TCyBufferedTransportFactory())
    server.serve()

    # gunicorn_thrift dolphin.service.douyin.kolserver:app -k thriftpy_sync -b 0.0.0.0:6000 -w 4 --thrift-protocol-factory thriftpy.protocol:TCyBinaryProtocolFactory --thrift-transport-factory thriftpy.transport:TCyBufferedTransportFactory --thrift-client-timeout=5
    # gunicorn_thrift dolphin.service.douyin.kolserver:app --bind 0.0.0.0:7000 -w 10 -k thriftpy_gevent  --timeout 10  --thrift-protocol-factory thriftpy2.protocol:TCyBinaryProtocolFactory --thrift-transport-factory thriftpy2.transport:TCyBufferedTransportFactory --error-logfile aa.log -D -p douyin_server.pid
예제 #9
0
# -*- coding: utf-8 -*-

import thriftpy2
from thriftpy2.thrift import TProcessor

pingpong = thriftpy2.load("pingpong.thrift")


class Dispatcher(object):
    def ping(self):
        print("ping pong!")
        return 'pong'


app = TProcessor(pingpong.PingService, Dispatcher())
예제 #10
0
def test_thrift_transport():
    test_thrift = thriftpy2.load("apache_json_test.thrift",
                                 module_name="test_thrift")
    Test = test_thrift.Test
    Foo = test_thrift.Foo
    test_object = Test(
        tbool=False,
        tbyte=16,
        tdouble=1.234567,
        tlong=123123123,
        tshort=123,
        tint=12345678,
        tstr="Testing String",
        tsetofints={1, 2, 3, 4, 5},
        tmap_of_int2str={
            1: "one",
            2: "two",
            3: "three"
        },
        tlist_of_strings=["how", "do", "i", "test", "this?"],
        tmap_of_str2foo={
            'first': Foo("first"),
            "2nd": Foo("baz")
        },
        tmap_of_str2foolist={'test': [Foo("test list entry")]},
        tmap_of_str2mapofstring2foo={"first": {
            "second": Foo("testing")
        }},
        tmap_of_str2stringlist={
            "words": ["dog", "cat", "pie"],
            "other": ["test", "foo", "bar", "baz", "quux"]
        },
        tfoo=Foo("test food"),
        tlist_of_foo=[Foo("1"), Foo("2"), Foo("3")],
        tlist_of_maps2int=[{
            "one": 1,
            "two": 2,
            "three": 3
        }],
        tmap_of_int2foo={
            1: Foo("One"),
            2: Foo("Two"),
            5: Foo("Five")
        },
        tbinary=b"\x01\x0fabc123\x00\x02")
    # A request generated by apache thrift that matches the above object
    request_data = b"""[1,"test",1,0,{"1":{"rec":{"1":{"tf":0},"2":{"i8":16},
    "3":{"i16":123},"4":{"i32":12345678},"5":{"i64":123123123},"6":
    {"dbl":1.234567},"7":{"str":"Testing String"},"8":{"lst":["str",5,
    "how","do","i","test","this?"]},"9":{"map":["i32","str",3,{"1":"one",
    "2":"two","3":"three"}]},"10":{"set":["i32",5,1,2,3,4,5]},
    "11":{"map":["str","rec",2,{"first":{"1":{"str":"first"}},"2nd":
    {"1":{"str":"baz"}}}]},"12":{"map":["str","lst",
    2,{"words":["str",3,"dog","cat","pie"],"other":["str",5,"test",
    "foo","bar","baz","quux"]}]},"13":{"map":["str",
    "map",1,{"first":["str","rec",1,{"second":{"1":{"str":"testing"}}}]}]},
    "14":{"lst":["rec",3,{"1":{"str":"1"}},
    {"1":{"str":"2"}},{"1":{"str":"3"}}]},"15":{"rec":{"1":{
    "str":"test food"}}},"16":{"lst":["map",1,["str","i32",
    3,{"one":1,"two":2,"three":3}]]},"17":{"map":["str","lst",1,{"test":
    ["rec",1,{"1":{"str":"test list entry"}}]}]},
    "18":{"map":["i32","rec",3,{"1":{"1":{"str":"One"}},"2":{"1":
    {"str":"Two"}},"5":{"1":{"str":"Five"}}}]},
    "19":{"str":"AQ9hYmMxMjMAAg=="}}}}]"""

    class Handler:
        @staticmethod
        def test(t):
            # t should match the object above
            assert recursive_vars(t) == recursive_vars(test_object)
            return t

    tp2_thrift_processor = TProcessor(test_thrift.TestService, Handler())
    tp2_factory = TApacheJSONProtocolFactory()
    iprot = tp2_factory.get_protocol(TMemoryBuffer(request_data))
    obuf = TMemoryBuffer()
    oprot = tp2_factory.get_protocol(obuf)

    tp2_thrift_processor.process(iprot, oprot)

    # output buffers should be the same
    final_data = obuf.getvalue()
    assert json.loads(request_data.decode('utf8'))[4]['1'] == \
           json.loads(final_data.decode('utf8'))[4]['0']
예제 #11
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os

import thriftpy2
from thriftpy2.thrift import TProcessor

pingpong_thrift = thriftpy2.load(
    os.path.join(os.path.dirname(__file__), "pingpong.thrift"))
PingService = pingpong_thrift.PingService


class PingpongServer(object):
    def ping(self):
        if os.environ.get('about_to_shutdown') == '1':
            raise pingpong_thrift.AboutToShutDownException
        return "pong"


app = TProcessor(pingpong_thrift.PingService, PingpongServer())
예제 #12
0
# _*_coding:utf-8_*_
# __author: g
import os

import thriftpy2
from thriftpy2.thrift import TProcessor

tp = thriftpy2.load(os.path.join(os.path.dirname(__file__), "hello.thrift"))

service = tp.TestService  # *.thrift文件的 server名


class TestServer:
    def hello(self, msg):
        # time.sleep(2)
        return f"{msg}, hello"


app = TProcessor(tp.TestService, TestServer())