windows下thrift的使用(python)

23 篇文章 1 订阅

转:https://www.cnblogs.com/pinking/p/7726478.html

1、下载thrift,下载地址:http://archive.apache.org/dist/thrift/0.9.3/

2、在编写python的thrift代码时,需要先安装thrift module,下载路径:https://pypi.python.org/pypi/thrift/0.9.1

3、安装thrift:在C盘新建一个Thtift文件夹,将下载的thrift-0.9.3.exe重新命名为thrift.exe后放到Thtift文件夹下,将该路径添加到环境变量,检验是否安装成功:在命令行中输入:

即可以看到安装后的版本信息。

E:\编程开发\thrift>thrift-0.12.0.exe --version
Thrift version 0.12.0

4、thrift 采用IDL(Interface Definition Language)来定义通用的服务接口,并通过生成不同的语言代理实现来达到跨语言、平台的功能。在thrift的IDL中可以定义以下一些类型:基本数据类型,结构体,容器,异常、服务。

thrift脚本:

const string HELLO_YK = "yk"
service HelloWorld {
void ping(),
string sayHello(),
string sayMsg(1:string msg)
}

thrift脚本通过Thrift编辑器生成所要求的python开发语言代码。即:

E:\编程开发\thrift>thrift-0.12.0.exe -r -gen py helloworld.thrift

5、Thrift是一个典型的CS结构,客户端和服务端可以使用不同的语言开发。本文以python为例:

PythonServer.py:

import sys
sys.path.append('./gen-py')
  
from helloworld import HelloWorld
from helloworld.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 HelloWorldHandler:
  def __init__(self):
    self.log = {}
 
  def ping(self):
    print "ping()"
 
  def sayHello(self):
    print "sayHello()"
    return "say hello from " + socket.gethostbyname(socket.gethostname())
 
  def sayMsg(self, msg):
    print "sayMsg(" + msg + ")"
    return "say " + msg + " from " + socket.gethostbyname(socket.gethostname())
 
handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket('127.0.0.1',30303)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
 
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
 
print "Starting python server..."
server.serve()
print "done!"

PythonClient.py

import sys
sys.path.append('./gen-py')
 
from helloworld import HelloWorld
from helloworld.ttypes import *
from helloworld.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('127.0.0.1', 30303)
 
  # 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 = HelloWorld.Client(protocol)
 
  # Connect!
  transport.open()
 
  client.ping()
  print "ping()"
 
  msg = client.sayHello()
  print msg
  msg = client.sayMsg(HELLO_YK)
  print msg
 
  transport.close()
 
except Thrift.TException, tx:
  print "%s" % (tx.message)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值