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
    
  • 相关阅读:
    html问题记录20180529
    html问题记录20180518
    html问题记录20180515
    Redis持久化--AOF
    Redis持久化--RDB
    Redis事件模型
    两个字符串的编辑距离-动态规划方法
    Reactor事件模型在Redis中的应用
    事件驱动模式--Reactor
    IO多路复用--总结
  • 原文地址:https://www.cnblogs.com/caitian/p/9306269.html
Copyright © 2011-2022 走看看