编辑接口文件 hellowworld.thrift

service HelloWorld {
string ping(),
string say(1:string msg)
}

编辑 server.py

#!/usr/bin/env python

import socket
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 class HelloWorldHandler:
def ping(self):
return "pong" def say(self, msg):
ret = "Received: " + msg
print ret
return ret handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket("localhost", 9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print "Starting thrift server in python..."
server.serve()
print "done!"

编辑
client.py

#!/usr/bin/env python

import sys
sys.path.append('./gen-py') from helloworld import HelloWorld from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol try:
transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = HelloWorld.Client(protocol)
transport.open() print "client - ping"
print "server - " + client.ping() print "client - say"
msg = client.say("Hello!")
print "server - " + msg transport.close() except Thrift.TException, ex:
print "%s" % (ex.message)

运行:

thrift --gen py helloworld.thrift
python server.py
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. PHP删除符合条件的整个目录

    <?php /** * @name delFile函数与delDir函数一起使用, 删除符合条件的整个目录 * @param string $path 指定操作路径 * @return null ...

  2. p39

    ; ========================================== ; pmtest2.asm ; 编译方法:nasm pmtest2.asm -o pmtest2.com ; ...

  3. jQuery:节点(插入,复制,替换,删除)操作

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

    ***************************错误提示************************************************ SEVERE: A child cont ...

  5. JAXL发送房间消息

    使用composer形式安装的JAXL <?php require_once "vendor/autoload.php"; $client = new JAXL(array( ...

  6. ASP.NET Ajax简单的无刷新分页

    最近练习了一些AJAX无刷新分页,写得比较简单,性能不知道怎么样,求大神指点,如有更好的分页提供,欢迎交流! 发话不多说了,直接上代码! 首先从网上下了一个JS分页,感觉挺好用的 (function( ...

  7. Jquery 根据value值设置下拉列表(select)默认选中项

    方法一: $("#selIndustyType option[value='1']").attr("selected", "selected" ...

  8. java web服务器文件的下载(有下载弹出匡)

    昨天做了一个文件从服务下载的功能,怎么都不弹出页面,下载框.后查询得知.目前两种方法 1.<a href='下载路径' /> 2.window.location.href = basePa ...

  9. if条件判断语句的不同

    let number = ["a":1, "b":2, "c":3]; if let num = number["d"] ...

  10. 【学习笔记】【C语言】常量

    1. 什么是常量 常量,表示一些固定的数据 2. 常量的分类 1> 整型常量(int) 包括了所有的整数,比如6.27.109.256.-10.0.-289等 2> 浮点型常量(float ...