Python&Thrift--Server&Client
2015-12-01 19:28
609 查看
thrift目前只支持python2.6+,但不支持3.XX版本。
thrift下载:http://thrift.apache.org/
安装thrift:
Java代码
./configure
make
make install
安装python的thrift组件
Java代码
cd /usr/local/thrift-0.9.1/lib/py
python setup.py install
thrift模版文件PythonService.thrift
Java代码
service PythonService{
string get(1:i32 id)
i32 remove(1:i32 id)
}
生成python文件
Java代码
thrift --gen py PythonService.thrift
将gen-py/PythonService路径下的全部文件拷贝到自己的项目路径下,比如PythonThrift\servicePy
server端:
PythonThrift\server\PythonServiceServer.py
Java代码
# 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
Java代码
# 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下载:http://thrift.apache.org/
安装thrift:
Java代码
./configure
make
make install
安装python的thrift组件
Java代码
cd /usr/local/thrift-0.9.1/lib/py
python setup.py install
thrift模版文件PythonService.thrift
Java代码
service PythonService{
string get(1:i32 id)
i32 remove(1:i32 id)
}
生成python文件
Java代码
thrift --gen py PythonService.thrift
将gen-py/PythonService路径下的全部文件拷贝到自己的项目路径下,比如PythonThrift\servicePy
server端:
PythonThrift\server\PythonServiceServer.py
Java代码
# 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
Java代码
# 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()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法