zoukankan      html  css  js  c++  java
  • 【一】调通单机版的thrift-python版本

    开发步骤说明

    【任务1】调通单机版的thrift-python版本

    安装thrift

    • 下载源码包
      wget http://apache.fayea.com/thrift/0.9.3/thrift-0.9.3.tar.gz

    • 安装thrift [进入thrift目录]

      • 运行命令: yum install openssl-devel.x86_64 [可选择是否安装]
      • 运行命令: yum install boost-devel.x86_64 [必须安装]
      • 运行命令: ./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --with-php --without-php_extension --without-ruby --without-haskell --without-go
      • 运行命令: yum install boost-devel-static
      • 运行命令: make,make isntall
    • 安装thrift对python的支持模块
      pip install thrift==0.9.3

    创建thrift模块文件并编译

    1. 创建RecSys.thrift文件 [创建到任意目录]
    service RecSys {
        string rec_data(1:string data)
    }
    
    1. 命令:thrift --gen py RecSys.thrift,此时在当前目录中会产生gen-py的文件夹,其中有当前的模块名字

    开发python版的client和server

    • 创建 server.py 文件测试
    import sys
    sys.path.append('../schema/gen-py/')
    
    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    from RecSys import RecSys
    from thrift.server import TServer
    from RecSys.ttypes import *
    
    class RecSysHandler(RecSys.Iface):
        def rec_data(self, a):
            print "Receive: %s" %(a)
            return "I`m OK !!!"
    
    if __name__ == "__main__":
    
        # 实例化handler
        handler = RecSysHandler()
    
        # 设置processor
        processor = RecSys.Processor(handler)
            
        # 设置端口
        transport = TSocket.TServerSocket('localhost', port=9900)
                    
        # 设置传输层
        tfactory = TTransport.TBufferedTransportFactory()
                            
        # 设置传输协议
        pfactory = TBinaryProtocol.TBinaryProtocolFactory()
        handler = RecSysHandler()
    
        server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
    
        print 'Starting the server...'
        server.serve()
        print 'done'
    
    • 创建 client.py 文件测试
    #coding=utf-8
    
    import sys
    sys.path.append('../schema/gen-py/')
    
    from thrift import Thrift
    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    from RecSys import RecSys
    
    if __name__ == "__main__":
    
        # 设置端口
        transport = TSocket.TSocket('localhost', port=9900)
                    
        # 设置传输层
        transport = TTransport.TBufferedTransport(transport)
                            
        # 设置传输协议
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
    
        client = RecSys.Client(protocol)
    
        transport.open()
    
        rs = client.rec_data("are you ok ??")
        print "receive return data: ", rs
    
        transport.close()
    
  • 相关阅读:
    vs调试技巧
    ubuntu中安装Docker
    Docker的简单认知
    mysql笔记01
    5款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具绿色版
    怎么用HD Tune检测硬盘坏道
    Dreamweaver_CS6安装与破解
    Microsoft Office 2013 (64位) 免费完整版(安装 + 激活)
    Microsoft office2007免费版下载(安装 + 破解)
    OneNote如何使用
  • 原文地址:https://www.cnblogs.com/screen/p/9481622.html
Copyright © 2011-2022 走看看