windows下thrift的使用(python)
1、下载thrift,下载地址:http://archive.apache.org/dist/thrift/0.9.3/
2、在编写python的thrift代码时,需要先安装thrift module,下载路径:https://pypi.python.org/pypi/thrift/0.9.1
3、安装thrift:在C盘新建一个Thtift文件夹,将下载的thrift-0.9.3.exe重新命名为thrift.exe后放到Thtift文件夹下,将该路径添加到环境变量,检验是否安装成功:在命令行中输入:

即可以看到安装后的版本信息。
4、thrift 采用IDL(Interface Definition Language)来定义通用的服务接口,并通过生成不同的语言代理实现来达到跨语言、平台的功能。在thrift的IDL中可以定义以下一些类型:基本数据类型,结构体,容器,异常、服务。
thrift脚本:
const string HELLO_YK = "yk"
service HelloWorld {
void ping(),
string sayHello(),
string sayMsg(1:string msg)
}
thrift脚本通过Thrift编辑器生成所要求的python开发语言代码。即:

5、Thrift是一个典型的CS结构,客户端和服务端可以使用不同的语言开发。本文以python为例:
PythonServer.py:
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 import socket class HelloWorldHandler:
def __init__(self):
self.log = {} def ping(self):
print "ping()" def sayHello(self):
print "sayHello()"
return "say hello from " + socket.gethostbyname(socket.gethostname()) def sayMsg(self, msg):
print "sayMsg(" + msg + ")"
return "say " + msg + " from " + socket.gethostbyname(socket.gethostname()) handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket('127.0.0.1',30303)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TSimpleServer(processor, transport, tfactory, pfactory) print "Starting python server..."
server.serve()
print "done!"
PythonClient.py
import sys
sys.path.append('./gen-py') from helloworld import HelloWorld
from helloworld.ttypes import *
from helloworld.constants import * from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol try:
# Make socket
transport = TSocket.TSocket('127.0.0.1', 30303) # Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport) # Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport) # Create a client to use the protocol encoder
client = HelloWorld.Client(protocol) # Connect!
transport.open() client.ping()
print "ping()" msg = client.sayHello()
print msg
msg = client.sayMsg(HELLO_YK)
print msg transport.close() except Thrift.TException, tx:
print "%s" % (tx.message)
运行结果:


文件下载:
windows下thrift的使用(python)的更多相关文章
- Windows下使用pip安装python包是报错-UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0
先交待下开发环境: 操作系统:Windows 7 Python版本:2.7.9 Pip版本:6.1.1 其他环境忽略 在windows下使用pip下载python包,出现如下错误 Collecting ...
- 全网最全的Windows下Anaconda2 / Anaconda3里Python语言实现定时发送微信消息给好友或群里(图文详解)
不多说,直接上干货! 缘由: (1)最近看到情侣零点送祝福,感觉还是很浪漫的事情,相信有很多人熬夜为了给爱的人送上零点祝福,但是有时等着等着就睡着了或者时间并不是卡的那么准就有点强迫症了,这是也许程序 ...
- 在windows下使用多版本Python安装相应的虚拟开发环境
在windows下面使用多版本Python安装相应的虚拟开发环境 在搭建一个项目的时候,希望使用最新版的Python3.7版本,但是Python3.6的版本也要留下,那么问题来了,如何解决这个问题 ...
- windows下使用virtualenv对python进行多版本隔离开发
1.windows下安装virtualenv pip install virtualenv 2.进入项目目录,创建虚拟环境,例如: virtualenv venv (默认python版本) virtu ...
- windows下使用mingw编译python扩展模块
环境: 1.python2.7.2 2.mingw20120426 说明: PYTHON_HOME:不做特殊说明的话,默认代表python的安装路径. MINGW_HOME:不做特殊说明的话,默认代表 ...
- windows下使用pip安装python的第三方lxml库
lxml是Python语言里和XML以及HTML工作的功能最丰富和最容易使用的库.lxml库的安装和python其他第三方库的安装方法是一样的,只是可能由于一些细节上的失误导致安装失败. 工具 Pyt ...
- Windows下Eclipse+PyDev配置Python开发环境
1.简介 Eclipse是一款基于Java的可扩展开发平台.其官方下载中包括J2EE.Java.C/C++.Android等诸多版本.除此之外,Eclipse还可以通过安装插件的方式进行包括Pytho ...
- 解决 windows下安装Anaconda后python pip不可用的情况
在windows系统下通过安装Anaconda的方式安装的python使用中发现不能再通过pip安装python包.只能通过conda install packname 的方法,导致很多conda不支 ...
- windows下thrift的使用(C++)
thrift cpp环境搭建: 1. 安装boost_1_53_0,注意,使用vs2010版本时,使用二进制的boost安装版本,生成的lib有可能是,在后续操作会出问题.在源码目录中,运行boot ...
随机推荐
- Python练习-生成器-一个生成器被坑的体无完肤
代码如下,尽可能独立阅读: # 编辑者:闫龙 from urllib.request import urlopen #导入一个包,这就是egon留的一个坑 def get(url):#这是为了保证题目 ...
- Android选择头像
http://www.jianshu.com/p/8b3e78046c1c 注意:在Android6.0之后,使用相机拍照需要权限 在选择头像使用相机拍摄时添加以下代码即可. Acp.getInsta ...
- mysql取字段名注意事项!!!!千万不能和关键字同名
今天就碰到一个恶心的问题,更新时update sql语句报错,查了半天感觉没问题啊,后来一行一行定位,终于找到原因了, 原来是有个字段是show,和mysql关键字冲突了,坑爹! 改了个名字就好了,或 ...
- ARM Linux 3.x的设备树(Device Tree)【转】
转自:http://blog.csdn.net/21cnbao/article/details/8457546 宋宝华 Barry Song <21cnbao@gmail.com> 1. ...
- checkbox 全选和取消
//全选 $("#checkall").click(function () { if (this.checked) { //如果当前点击的多选框被选中 $('input[type= ...
- TCP/IP详解(整理)
1.概述 路由器是在网络层进行联通,而网桥是在链路层联通不同的网络. IP层用ICMP来与其他主机或路由器交换错误报文和其他的重要信息.应用程序也可以访问ICMP,两个诊断工具:Ping和Tracer ...
- linux bash shell之declare
一. #Set the right GC options based on the what we are runningdeclare -a server_cmds=("master&qu ...
- 缓存数据库-redis介绍
一:Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的 ...
- 虚拟机 windows xp sp3 原版
原版的镜像:http://www.7xdown.com/Download.asp?ID=3319&URL=http://d5.7xdown.com/soft2/&file=Window ...
- git —— 分支
git中每一个分支相当于一个时间线 并列且相互平行 控制用指针控制~ 1.第一种创建命令: $ git branch 分支名称 —— 创建分支 $ git checkout 分支名称 —— 切换分支 ...