zoukankan      html  css  js  c++  java
  • 用thrift搭一个单进程服务

    首先写一个服务的功能类,例如print 一些东西

    HelloHandler.py

    #!/bin/python
    #encoding=utf8
    
    import os
    import sys
    import json
    import hashlib
    import logging
    import time
    import random
    import traceback
    import signal
    
    import re
    from datetime import datetime
    
    cur_dir = os.path.dirname(os.path.abspath(__file__))
    
    #加载thrift接口的路径
    thrift_path = os.path.sep.join([cur_dir, 'utils', 'thrift', 'gen-py'])
    sys.path.append(thrift_path)
    
    
    class HelloHandler():
        def __init__(self):
            self.name = None
    
    
        def HelloWorld(self,name):
            self.name = name
            print name
            return name
    
        
    if __name__ == '__main__':
        name = "haha"
        tt_obj = HelloHandler()
        tt_obj.HelloWorld(name)
    time.sleep(3)
    

    服务端程序:Hello_server.py

    #!/bin/python
    #encoding=utf8
    
    import os
    import sys
    import json
    import hashlib
    import logging
    import pymongo
    import time
    import traceback
    import signal
    import urllib
    import urllib2
    import mimetypes
    import re
    import ConfigParser
    from datetime import datetime
    from bson import json_util
    from collections import defaultdict
    from multiprocessing import Process
    
    sys.path.append("../utils/thrift/gen-py")
    from HelloHandler import HelloHandler
    from IE_service import IEService
    
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    from thrift.protocol import TCompactProtocol
    from thrift.server import TServer
    import HelloHandler
    
    cur_dir = os.path.dirname(os.path.abspath(__file__))
    cf = ConfigParser.ConfigParser()
    conf_fn = os.path.sep.join([cur_dir, '../conf', 'thrift.conf'])
    port = cf.read(conf_fn)
    port = int(cf.get('thrift', 'port')) #这里设置端口
    
    if __name__ == '__main__':
        '''
                    level=logging.INFO 
                    level=logging.WARNING 
                    filename=cur_dir+'/var/SF_server.log' 
        '''
        logging.basicConfig( 
                    level=logging.WARNING 
                    , format='%(thread)d %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' 
                )
        handler = HelloHandler()
        processor = IEService.Processor(handler)
        transport = TSocket.TServerSocket(port=port)
        tfactory = TTransport.TBufferedTransportFactory()
        pfactory = TBinaryProtocol.TBinaryProtocolFactory()
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
        print 'starting server'
        server.serve()
        manager.join()
        print 'done'
    

    IE_server.thrift

    namespace py IE_service

    service IEService

        string HelloWorld(1:string req_json);

    }

    客户端程序

    Client.py

    #!/bin/python
    #encoding=utf8
    import sys
    sys.path.append('./gen-py')
    from IE_service import *
    from IE_service import IEService
    
    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    import json
    try:
        transport = TSocket.TSocket('localhost', 62127) #端口为服务端设置的端口
        transport = TTransport.TBufferedTransport(transport)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        client = IEService.Client(protocol)
        transport.open()
        req_dic = {"req_type":"type_doc", 
                   "file_link":"/home/op/app/Project/msword-parser-server/bin/data/K0001-A.docx",
                  }
        print 'start'
        res = client.HelloWorld(json.dumps(req_dic))
        print res
        transport.close()
    except Thrift.TException as e:
        print 'exceptino'
        print e
    
  • 相关阅读:
    angularjs的$on、$emit、$broadcast
    angularjs中的路由介绍详解 ui-route(转)
    ionic入门教程-ionic路由详解(state、route、resolve)(转)
    Cocos Creator 加载使用protobuf第三方库,因为加载顺序报错
    Cocos Creator 计时器错误 cc.Scheduler: Illegal target which doesn't have uuid or instanceId.
    Cocos Creator 构造函数传参警告 Can not instantiate CCClass 'Test' with arguments.
    Cocos Creator 对象池NodePool
    Cocos Creator 坐标系 (convertToWorldSpaceAR、convertToNodeSpaceAR)
    Cocos Creator 常驻节点addPersistRootNode
    Cocos Creator 配合Tiled地图的使用
  • 原文地址:https://www.cnblogs.com/caitian/p/9306269.html
Copyright © 2011-2022 走看看