CentOS thrift python demo
编辑接口文件 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的更多相关文章
- Linux CentOS下Python+robot framework环境搭建
Linux CentOS下Python+robot framework环境搭建 by:授客 QQ:1033553122 操作系统环境:CentOS 6.5-x86_64 下载地址:http://w ...
- Centos搭建Python+Nginx+Tornado+Mysql环境[转载]
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入 ...
- centos 更新python
1.CentOS安装Python的依赖包 yum groupinstall "Development tools"yum install zlib-devel bzip2-deve ...
- 转: 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/ ...
- CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法
CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法如下: 截至包子写本文的时候,pip最新为 1.5.5 wget --no-check-certificate h ...
- 在阿里云服务器上配置CentOS+Nginx+Python+Flask环境
在阿里云服务器上配置CentOS+Nginx+Python+Flask环境 项目运行环境 阿里云(单核CPU, 1G内存, Ubuntu 14.04 x64 带宽1Mbps), 具体购买和ssh连接阿 ...
- Linux CentOS下Python+robot framework环境搭建
转载自:http://blog.sina.com.cn/s/blog_13cc013b50102vof1.html 操作系统环境:CentOS 6.5-x86_64 下载地址:http://www.c ...
- 【转】Centos升级Python 2.7.12并安装pip、ipython
Centos系统一般默认就安装有Python2.6.6版本,不少软件需要2.7以上的,通过包管理工具安装不了最新的版本,通过源码编译可以方便安装指定版本,只需要把下面版本的数字换成你想要的版本号. 1 ...
- 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 ...
随机推荐
- java jdbc连接mysql
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- 如何让oracle的select强制走索
大多数情况下,oracle数据库内置的查询优化策略还是很成功的,但偶尔也有糊涂的时候,即使有索引,也会做全表扫描,可以参考以下语句的写法,强制让select语句使用索引. (注意:走索引不一定是最优的 ...
- 关于VIM在Win10下的无意义折腾
这两天和VIM较上劲了,总结下来其实是极无意义的折腾,浪费了很多宝贵的时间! 事情是这样的,无意中发现vim在Win10的控制台窗口无参数打开时不显示那个“乌干达儿童”的界面(其实就是:intro的内 ...
- 剑指Offer45 约瑟夫环
/************************************************************************* > File Name: 45_LastNu ...
- 实现多个UIView之间切换的动画效果
@interface RootViewController (){ UIView *view1; UIView *view2; int flag; } @end @implementation Roo ...
- Things make us different
Don't just accumulate knowledge, apply it. You will never really know or understand something until ...
- VHDL操作运算符的优先级顺序
- html有序列表和无序列表
css控制UL LI 的样式详解(推荐) CSS: 代码如下: #menu ul {list-style:none;margin:0px;} #menu ul li {float:left;} 代码如 ...
- 【Unity3D】场景转换与退出
1.场景转换 : 老版本的场景切换用的是Application.LoadLevel([场景名字或者在File->Build settings里面的场景代号]); 新版本的场景转换用到了Scene ...
- UI4_UITableViewSectionIndex
// AppDelegate.m // UI4_UITableViewSectionIndex // // Created by zhangxueming on 15/7/14. // Copyrig ...