`
hanqunfeng
  • 浏览: 1529704 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Python&Thrift--Server&Client

 
阅读更多

thrift目前只支持python2.6+,但不支持3.XX版本。

thrift下载:http://thrift.apache.org/

安装thrift:

./configure
make
make install

 安装python的thrift组件

cd /usr/local/thrift-0.9.1/lib/py
python setup.py install

 

thrift模版文件PythonService.thrift

service PythonService{  
	string get(1:i32 id)  
	i32 remove(1:i32 id)  
}  

 

生成python文件

thrift --gen py PythonService.thrift

将gen-py/PythonService路径下的全部文件拷贝到自己的项目路径下,比如PythonThrift\servicePy

 

server端:

PythonThrift\server\PythonServiceServer.py

# coding=utf-8
'''
Created on 2013-9-22

@author: hanqunfeng
'''

import sys
sys.path.append('../') #导入上下文环境

from servicePy import  PythonService
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TCompactProtocol
from thrift.server import TServer


import socket
# 实现类
class PythonServiceServer:
     def get(self, id):
         print socket.gethostbyname(socket.gethostname())
         return "get=="+str(id)
     
     def remove(self, id):
         print socket.gethostbyname(socket.gethostname())
         return id
     
handler = PythonServiceServer()
# 注册实现类
processor = PythonService.Processor(handler)
transport = TSocket.TServerSocket('localhost',30303)
tfactory = TTransport.TBufferedTransportFactory()
# pfactory = TBinaryProtocol.TBinaryProtocolFactory()
pfactory = TCompactProtocol.TCompactProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

print "Starting python server..."
server.serve()
print "done!"    

 

client端:

PythonThrift\client\PythonServiceClient.py

# coding=utf-8
'''
Created on 2013-9-22

@author: hanqunfeng
'''

import sys
sys.path.append('../') #导入上下文环境

from servicePy import  PythonService
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TCompactProtocol

def pythonServerExe():
    try:
        transport = TSocket.TSocket('localhost', 30303) 
        transport = TTransport.TBufferedTransport(transport)
        # protocol = TBinaryProtocol.TBinaryProtocol(transport)
        protocol = TCompactProtocol.TCompactProtocol(transport)
        client = PythonService.Client(protocol)
        transport.open()
        print "The return value is : " 
        print client.remove(12)
        print client.get(100)
        print "............"
        transport.close()
    except Thrift.TException, tx:
        print '%s' % (tx.message)
        
        
if __name__ == '__main__':
    pythonServerExe()

 

分享到:
评论

相关推荐

    thrift-python-java:一个简单的基于Java的Thrift Service服务器和一个Java&Python客户端

    cd java-server-client mvn test -Pserver 此命令将在端口9090上运行服务器 运行Java客户端 cd java-server-client mvn test -Pclient 运行Python客户端 首先,设置所需的依赖项 git clone ...

    Learning.Apache.Thrift.178588274

    Make applications cross-communicate using Apache Thrift! About This Book Leverage Apache Thrift to enable ...Chapter 7: An Example Client-Server Application Chapter 8: Advanced Usage of Apache Thrift

    Thrift的Python版本ThriftPy.zip

    ThriftPy 是 Apache Thrift 的 Python 语言移植版本。 服务器端示例代码: import thriftpy from thriftpy.rpc import make_server pingpong = thriftpy.load("pingpong.thrift") class Dispatcher(object)...

    Hadoop之Hbase从入门到精通 .doc

    * Thrift Gateway:利用 Thrift 序列化技术,支持 C++、PHP、Python 等多种语言,适合其他异构系统在线访问 HBase 表数据。 * REST Gateway:支持 REST 风格的 Http API 访问 HBase,解除了语言限制。 * Pig:可以...

    使用thrift简单模拟一个游戏玩家匹配系统

    使用thrift实现一个简单的游戏匹配系统 游戏端使用Python 服务器端使用C++ 根据分差匹配能力相近的玩家,当匹配池中玩家数量较少 时会根据等待时间,...定义接口 2.server 3.client thrift相当于不同进程间的有向边

    FusionInsightHD华为大数据平台.pdf

    "Supervisor Process"和"WebServer"通过"THRIFT/REST"接⼝与WebServer上的应⽤进⾏交 互,如图所⽰。 Loader 实现FusionInsight HD与关系型数据库、⽂件系统之间交换数据和⽂件的数据加载⼯具;同时也可以将数据从...

    DDMQ消息队列-其他

    carrera-producer 生产消息代理模块,内置 Thrift Server, 负责将 client 的生产的消息转发给 broker。 carrera-consumer 消费消息代理模块, 内置 Thrift Server, 提供 SDK 拉取和 HTTP 推送等方式将消息发给订阅...

    java代理服务器源码-DDMQ:DDMQ是低延迟、高吞吐、高可用的分布式消息传递产品

    Client,开发者可以体验到最简单稳定的消息生产和消费。 特征 消息传递模型:同时支持 P2P 和 Pub/Sub 消息传递模型 海量消息存储,同时支持RocketMQ和Kafka作为存储引擎 低延迟和高吞吐量 延迟消息,使用 RocksDB ...

    AddoptNginxThirftTest.rar

    通过网页远程Client一个请求加法,服务器Server收到请求后,启动计算函数计算,然后把结果通过json格式通过网页返还给并呈现在网页上

    Go RPC开发指南.pdf

    Go RPC开发指南首先介绍了使用Go官方库开发RPC服务的方法,然后介绍流行的gRPC库以及其它一些RPC框架如Thrift等,后面重点介绍高性能的分布式全功能的RPC框架rpcx。读者通过阅读本书,可以快速学习和了解Go生态圈的...

Global site tag (gtag.js) - Google Analytics