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
    
  • 相关阅读:
    预处理器宏指令(Macro)
    汇编语言中macro的用法
    USB设备的VID与PID
    前端工具 | JS编译器Monaco使用教程
    vue + ts中的shimsvue.d.ts文件的作用,在ts中引入vueecharts等vue文件 TypeScript 导入 JSON Module resolveJsonModule
    Jenkins自动打包并部署到远程服务器
    如何获取设备的VID,PID?
    TypeScript装饰器(decorators)
    MACRO指令
    IE6左右边框断线现象
  • 原文地址:https://www.cnblogs.com/caitian/p/9306269.html
Copyright © 2011-2022 走看看