编辑接口文件 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. java8中的Stream

    Collection.stream() / parallelStream() 1. Stream 1)Filter    stringCollection .stream().filter((s) - ...

  2. 【MongoDB】增删改查基本操作

    查看所有数据库 show dbs 切换数据库(若不存在,会自动创建) use databasename 删除当前数据库 db.dropDatabase() MongoDB中没有表,只有集合. 插入集合 ...

  3. Ubuntu(16.04) 下如何修改(安装)arm-linux-gcc编译器

    ubuntu下如何修改(安装)arm-linux-gcc编译器 将gcc解压到根目录 sudo tar xjf arm-linux-gcc-4.3.2.tar.bz2 -C / 查看原来的环境变量 e ...

  4. page61-将二分查找重写为一段面向对象的程序

    1 将二分查找重写为一段面向对象的程序 (用于在整数集合中进行查找的一种抽象数据类型) public class StaticSETofInts [API] StaticSETofInts(int[] ...

  5. linux 文件系统(inode和block)

    linux文件系统(inode block superblock)   先说一下格式化:每种操作系统所设置的文件属性/权限并不相同,为了存放这些文件所需的数据,因此就需要将分区格式化,以成为操作系统能 ...

  6. [转]Web Services使用out参数

    本文转自:http://www.cnblogs.com/zhaozhan/archive/2010/10/25/1860837.html Web Services使用out参数,在SOAP协议中会跟返 ...

  7. CENTOS如何禁用ROOT本地或远程SSH登录

    下面详细描述如何禁止root登录. 禁止root本地登录 禁止root远程ssh登录 禁止root本地登录 修改/etc/pam.d/login文件增加下面一行   1 auth required p ...

  8. Terminate program hitting CTRl+C within GDB

    Q: My program is determined to stop its execution by hitting CTRL+C in command window. By now, i hav ...

  9. C# 学习笔记03 DataTable

    1. DataTable 类对象表示一个内存中数据表.可以用来存放从数据库得到的DataSet. DataTable dt = SqlHelper.ExecuteDataTable(parameter ...

  10. Objective-C 【autorelease基本使用】

    ------------------------------------------- NSString中的内存管理问题 由于autoreleasepool的存在,对于内存管理就会很复杂,retain ...