Thrift Python in windows

一:安装

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/


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值