编辑接口文件 hellowworld.thrift

  1. service HelloWorld {
  2. string ping(),
  3. string say(1:string msg)
  4. }

编辑 server.py

  1. #!/usr/bin/env python
  2.  
  3. import socket
  4. import sys
  5. sys.path.append('./gen-py')
  6.  
  7. from helloworld import HelloWorld
  8. from helloworld.ttypes import *
  9.  
  10. from thrift.transport import TSocket
  11. from thrift.transport import TTransport
  12. from thrift.protocol import TBinaryProtocol
  13. from thrift.server import TServer
  14.  
  15. class HelloWorldHandler:
  16. def ping(self):
  17. return "pong"
  18.  
  19. def say(self, msg):
  20. ret = "Received: " + msg
  21. print ret
  22. return ret
  23.  
  24. handler = HelloWorldHandler()
  25. processor = HelloWorld.Processor(handler)
  26. transport = TSocket.TServerSocket("localhost", 9090)
  27. tfactory = TTransport.TBufferedTransportFactory()
  28. pfactory = TBinaryProtocol.TBinaryProtocolFactory()
  29.  
  30. server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
  31.  
  32. print "Starting thrift server in python..."
  33. server.serve()
  34. print "done!"

编辑
client.py

  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. sys.path.append('./gen-py')
  5.  
  6. from helloworld import HelloWorld
  7.  
  8. from thrift import Thrift
  9. from thrift.transport import TSocket
  10. from thrift.transport import TTransport
  11. from thrift.protocol import TBinaryProtocol
  12.  
  13. try:
  14. transport = TSocket.TSocket('localhost', 9090)
  15. transport = TTransport.TBufferedTransport(transport)
  16. protocol = TBinaryProtocol.TBinaryProtocol(transport)
  17. client = HelloWorld.Client(protocol)
  18. transport.open()
  19.  
  20. print "client - ping"
  21. print "server - " + client.ping()
  22.  
  23. print "client - say"
  24. msg = client.say("Hello!")
  25. print "server - " + msg
  26.  
  27. transport.close()
  28.  
  29. except Thrift.TException, ex:
  30. print "%s" % (ex.message)

运行:

  1. thrift --gen py helloworld.thrift
  2. python server.py
  3. python client.py #这个分一个窗口运行

如果修改里面的一个方法或者增加一个调用方法的话,需要在 helloword.thrift 里面定义函数及参数。

在服务端运行代码 thrift -r --gen py helloworld.thrift

重新生成 gen-py 文件夹,将里面的代码拷贝到客户端的服务器。

CentOS thrift python demo的更多相关文章

  1. Linux CentOS下Python+robot framework环境搭建

    Linux CentOS下Python+robot framework环境搭建   by:授客 QQ:1033553122 操作系统环境:CentOS 6.5-x86_64 下载地址:http://w ...

  2. Centos搭建Python+Nginx+Tornado+Mysql环境[转载]

    Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入 ...

  3. centos 更新python

    1.CentOS安装Python的依赖包 yum groupinstall "Development tools"yum install zlib-devel bzip2-deve ...

  4. 转: CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法

    from: http://www.linuxde.net/2014/05/15576.html CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法 2014/05/ ...

  5. CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法

    CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法如下: 截至包子写本文的时候,pip最新为 1.5.5 wget --no-check-certificate h ...

  6. 在阿里云服务器上配置CentOS+Nginx+Python+Flask环境

    在阿里云服务器上配置CentOS+Nginx+Python+Flask环境 项目运行环境 阿里云(单核CPU, 1G内存, Ubuntu 14.04 x64 带宽1Mbps), 具体购买和ssh连接阿 ...

  7. Linux CentOS下Python+robot framework环境搭建

    转载自:http://blog.sina.com.cn/s/blog_13cc013b50102vof1.html 操作系统环境:CentOS 6.5-x86_64 下载地址:http://www.c ...

  8. 【转】Centos升级Python 2.7.12并安装pip、ipython

    Centos系统一般默认就安装有Python2.6.6版本,不少软件需要2.7以上的,通过包管理工具安装不了最新的版本,通过源码编译可以方便安装指定版本,只需要把下面版本的数字换成你想要的版本号. 1 ...

  9. liunx CentOS 升级Python版本

    CentOS python版本是V2.6.6,升级3.4.3. 1.下载 安装包:wget http://www.python.org/ftp/python/3.4.3/Python-3.4.3.tg ...

随机推荐

  1. java jdbc连接mysql

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  2. 如何让oracle的select强制走索

    大多数情况下,oracle数据库内置的查询优化策略还是很成功的,但偶尔也有糊涂的时候,即使有索引,也会做全表扫描,可以参考以下语句的写法,强制让select语句使用索引. (注意:走索引不一定是最优的 ...

  3. 关于VIM在Win10下的无意义折腾

    这两天和VIM较上劲了,总结下来其实是极无意义的折腾,浪费了很多宝贵的时间! 事情是这样的,无意中发现vim在Win10的控制台窗口无参数打开时不显示那个“乌干达儿童”的界面(其实就是:intro的内 ...

  4. 剑指Offer45 约瑟夫环

    /************************************************************************* > File Name: 45_LastNu ...

  5. 实现多个UIView之间切换的动画效果

    @interface RootViewController (){ UIView *view1; UIView *view2; int flag; } @end @implementation Roo ...

  6. Things make us different

    Don't just accumulate knowledge, apply it. You will never really know or understand something until ...

  7. VHDL操作运算符的优先级顺序

           

  8. html有序列表和无序列表

    css控制UL LI 的样式详解(推荐) CSS: 代码如下: #menu ul {list-style:none;margin:0px;} #menu ul li {float:left;} 代码如 ...

  9. 【Unity3D】场景转换与退出

    1.场景转换 : 老版本的场景切换用的是Application.LoadLevel([场景名字或者在File->Build settings里面的场景代号]); 新版本的场景转换用到了Scene ...

  10. UI4_UITableViewSectionIndex

    // AppDelegate.m // UI4_UITableViewSectionIndex // // Created by zhangxueming on 15/7/14. // Copyrig ...