Thrift Python in windows

来源:互联网 发布:2017西安程序员工资 编辑:程序博客网 时间:2024/06/06 03:22

一:安装

1. 去官网下载thrift.exe
     http://thrift.apache.org/download/
 
2. 放到环境变量里面去
    在cmd中直接输入thrift可以运行。
 
3. 下载Thrift Python Software Library
    https://pypi.python.org/pypi/thrift/0.9.0
    
4. 解压,然后进入目录,执行:python setup.py install
 

二:测试

1. thrift文件

service Executer{bool add_measure_group(1: string str_addr, 2: i32 interval);bool del_measure_group(1: string str_addr, 2: i32 interval);}

2. thrift生成py

thrift -gen py calcute_server.thrift


3. 写server端程序

import syssys.path.append('./gen-py')from calcute_server import Executerfrom calcute_server.ttypes import *from thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom thrift.server import TServerimport socketclass ExecuterHandler:def __init__(self):self.log = {}def add_measure_group(self, addr, interval):print "add"return Truedef del_measure_group(self, addr, interval):print "del"return Truehandler = ExecuterHandler()processor = Executer.Processor(handler)transport = TSocket.TServerSocket("localhost",9000)tfactory = TTransport.TBufferedTransportFactory()pfactory = TBinaryProtocol.TBinaryProtocolFactory()server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)print "Starting python server..."server.serve()print "done!"


 

4. 写client端程序

import syssys.path.append('./gen-py')from calcute_server import Executerfrom calcute_server.ttypes import *from calcute_server.constants import *from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocoltry:# Make sockettransport = TSocket.TSocket('localhost', 9000)# Buffering is critical. Raw sockets are very slowtransport = TTransport.TBufferedTransport(transport)# Wrap in a protocolprotocol = TBinaryProtocol.TBinaryProtocol(transport)# Create a client to use the protocol encoderclient = Executer.Client(protocol)# Connect!transport.open()client.add_measure_group("ff15::1", 100)print 'done!'transport.close()except Thrift.TException, tx:print "%s" % (tx.message)


5. 目录结构

 

6. 结果

 

>python ExecuteServer.pyStarting python server...

 

>python ExecuteClient.pydone!

>python ExecuteServer.pyStarting python server...add


三:其他

其他语言thrift在windows上没有研究。

可以参考:http://thrift.apache.org/docs/install/windows/


 

原创粉丝点击