一:安装
1. 去官网下载thrift.exe
2. 放到环境变量里面去
在cmd中直接输入thrift可以运行。
3. 下载
Thrift Python Software Library
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 sys
sys.path.append('./gen-py')
from calcute_server import Executer
from calcute_server.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
import socket
class ExecuterHandler:
def __init__(self):
self.log = {}
def add_measure_group(self, addr, interval):
print "add"
return True
def del_measure_group(self, addr, interval):
print "del"
return True
handler = 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 sys
sys.path.append('./gen-py')
from calcute_server import Executer
from calcute_server.ttypes import *
from calcute_server.constants import *
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
try:
# Make socket
transport = TSocket.TSocket('localhost', 9000)
# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)
# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Create a client to use the protocol encoder
client = 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.py
Starting python server...
>python ExecuteClient.py
done!
>python ExecuteServer.py
Starting python server...
add
三:其他
其他语言thrift在windows上没有研究。
可以参考:http://thrift.apache.org/docs/install/windows/