转自:http://tkang.blogspot.com/2010/07/thrift-server-client-in-python.html

在编写python的thrift代码时,需要先安装thrift module

$ cd thrift-root/lib/py/
$ sudo python setup.py install

下面是一个python的例子 helloworld.thrift

const string HELLO_IN_KOREAN = "an-nyoung-ha-se-yo"const string HELLO_IN_FRENCH = "bonjour!"const string HELLO_IN_JAPANESE = "konichiwa!"service HelloWorld {void ping(),string sayHello(),string sayMsg(1:string msg)
}

生产代码

$ thrift -r --gen py helloworld.thrift

编写服务器PythonServer.py

#!/usr/bin/env pythonimport 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 TServerimport socketclass 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

#!/usr/bin/env pythonimport 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 TBinaryProtocoltry:# Make sockettransport = TSocket.TSocket('127.0.0.1', 30303)# 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 = HelloWorld.Client(protocol)# Connect!transport.open()client.ping()print "ping()"msg = client.sayHello()print msgmsg = client.sayMsg(HELLO_IN_KOREAN)print msgtransport.close()except Thrift.TException, tx:print "%s" % (tx.message)

运行程序

$ python PythonServer.py
$ python PythonClient.py

转载于:https://www.cnblogs.com/lovemdx/archive/2012/11/22/2782180.html

python thrift 示例相关推荐

  1. python把桢写入txt_ffmpeg 常用参数一览表及python 使用示例

    FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.提供了录制.转换以及流化音视频的完整解决方案. 它包含了非常先进的音频/视频编解码库libavcodec,这里介绍 ...

  2. python函数示例_PHP closeir()函数与示例

    python函数示例 PHP Closedir()函数 (PHP closedir() function) The full form of closedir is "Close Direc ...

  3. python函数示例_带Python示例的complex()函数

    python函数示例 Python complex()函数 (Python complex() function) complex() function is a library function i ...

  4. python函数示例_带Python示例的float()函数

    python函数示例 Python float()函数 (Python float() function) float() function is a library function in Pyth ...

  5. python函数示例_使用Python中的示例的input()函数

    python函数示例 Python input()函数 (Python input() function) input() function is a library function, it is ...

  6. python thrift 进行filter

    python thrift 进行filter 介于我百度的时候发现文档比较少 就写一下防止忘记 SingleColumnValueFilter('<family>', '<quali ...

  7. Python JSONPath示例

    什么是JSONPath? (What is JSONPath?) JSONPath is an expression language to parse JSON data. It's very si ...

  8. Python MySQL示例教程

    Welcome to Python MySQL example tutorial. MySQL is one of the most widely used database and python p ...

  9. python编程示例_Python套接字编程–服务器,客户端示例

    python编程示例 Good Day Learners! In our previous tutorial, we discussed about Python unittest module. T ...

  10. python代码设置超参数_超参数调优总结,贝叶斯优化Python代码示例

    本文介绍超参数(hyperparameter)的调优方法. 神经网络模型的参数可以分为两类,模型参数,在训练中通过梯度下降算法更新: 超参数,在训练中一般是固定数值或者以预设规则变化,比如批大小(ba ...

最新文章

  1. C语言模拟实现(三)-----独占型设备分配
  2. 局域网怎么查看单位摄像头_一行代码,实现多平台文件查看传输!
  3. T^T找数字(搜索+二进制枚举)
  4. 【FastDFS】分布式文件系统FastDFS之FastDHT文件去重
  5. 输出阻抗与输入阻抗详解
  6. 2018-2019年江苏省高等学校“阿里云大数据技术实战训练营”大学生万人计划学术冬令营开营...
  7. SDUT最短路径问题 1867————最短路
  8. 利用Python进行心脏病患者特征分析
  9. Excel一键创建班级成绩表模板的操作
  10. 4米乘以12米CAD图_身高1米6,却能在12顺位被选中?单场4分19助,他有多强?
  11. 你知道八皇后嘛?就是那个八皇后
  12. 软件测试教程 自动化测试appium篇
  13. [RFID]射频卡介绍(一)ID-EM4100
  14. 基于android平台多媒体播放器的设计与实现,基于Android的多媒体音乐播放器设计论文...
  15. linux交叉编译openssl,交叉编译openssl for linux arm-v5te-linux-gnueabi工具链
  16. 不要学计算机,学IT的我已经后悔了
  17. 携程 | 站会与公约
  18. 第三方框架简介ios
  19. OpenGL学习之二 -- 初探
  20. 微服务设计的四个原则

热门文章

  1. XMarks 书签王者
  2. YAML 有漏洞被弃用?网友:YAML 不背锅!
  3. 今天来了一位妹纸面试,我问她:线程池中多余的线程是如何回收的?她有点懵~...
  4. Spring Boot + PageHelper 实现分页,总结得很全了!
  5. Oracle用户可要顶住了:准备好大规模补丁工作!以修补多达 433 个的新安全漏洞...
  6. 自学鄙视链顶端的王者
  7. 数据结构实践课程设计【通讯录管理系统】
  8. [tldk][dpdk][dev] TLDK--基于dpdk的用户态协议栈传输层组件简单调研
  9. python的N个小功能(图片预处理:打开图片,滤波器,增强,灰度图转换,去噪,二值化,切割,保存)...
  10. 2017-2018-1 20155330 《信息安全系统设计基础》第3周学习总结