Python&Thrift--Server&Client
来源:互联网 发布:呼叫中心软件 编辑:程序博客网 时间:2024/06/15 23:10
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()
0 0
- Python&Thrift--Server&Client
- Thrift tutorial(python server and php client)
- thrift C++做server, C++,python, java做Client例子
- thrift C++做server, C++,python, java做Client例子
- thrift C++做server, C++,python, java做Client例子
- thrift源码研究-异步client&server
- thrift-异步client&server使用例子
- Spring集成Thrift--Server AND Client
- thrift-client异步+server非阻塞
- thrift语法及PHP-server-client demo
- Thrift c++ server & java client配置
- Thrift Server nodejs Client C# ---学习笔记
- thrift之Hello--python client & cpp sever
- client/server程序-python
- Python UDP Server Client
- python client and server
- python socket-server-client
- python server, client
- Leetcode日记(3)---Longest Substring Without Repeating Characters
- 接口 第一课(没注释)
- QTableWidget控件总结
- 《TCP/IP详解 卷1:协议》 读书笔记 第五章 RARP:逆地址解析协议
- Docker中级学习-Docker Registry
- Python&Thrift--Server&Client
- 第九章 定制应用程序外观
- 要成为一个牛逼程序猿,你要勇于尝试这10种姿势
- OOCSS(样式和结构的分离与容器和内容的分离)
- C/C++程序从编译到最终生成可执行文件的过程分析
- linux下查看文件编码及修改编码
- MATLAB常用数学函数
- 抽象类
- hello-world