编辑接口文件 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. 【MongoDB】使用mongo连接服务器。。。

    使用mongo连接服务器 命令行: ./mongo 主机号:端口号/数据库名 e.g. ./mongo 127.0.0.1:12345/mongodb1 关闭服务器 use admin db.shut ...

  2. Ubuntu16.04安装VMware Tools问题

    *************************************************************************** 问题:客户机操作系统已将 CD-ROM 门锁定, ...

  3. 第二回 认识CDN

    CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节 ...

  4. SVN Server导项目到本地库时提示"方法OPTIONS失败与无法连接到服务器"

    方法 OPTIONS 失败于 “https://xxxx/svn/xxxx”: 无法连接到服务器 (https://xxxx) 要留意  https 使用了443 端口,检查防火墙是否开放了该端口. ...

  5. ASP.NET MVC 4框架揭秘(微软6任MVP,高级软件顾问蒋金楠新作)

    http://www.cnblogs.com/artech/

  6. PAT (Basic Level) Practise (中文)1027. 打印沙漏(20)

    1027. 打印沙漏(20) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...

  7. iOS UILabel自定义行间距时获取高度

    本文介绍一下自定义行间距的UILabel的高度如何获取,需要借助一下开源的UILabel控件:TTTAttributedLabel 附下载地址 https://github.com/TTTAttrib ...

  8. Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()

    一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...

  9. UI2_UISwitch与UIActivity

    // // ViewController.m // UI2_UISwitch与UIActivity // // Created by zhangxueming on 15/7/7. // Copyri ...

  10. Optimistic Offline Lock乐观离线锁

    通过冲突检测和(发生冲突时的)事务回滚,来防止并发业务事务中的冲突. 通常一个业务事务的执行,会跨越一系列的系统事务. 一旦超出了单个系统事务的范围,就不能仅依靠DB管理程序来保证数据一致性. 乐观离 ...