1、编辑thrift服务文件
cd /home/wwwroot/thrift
mkdir python
cd python
vi message.thrift
注:message.thrift内容
namespace java com.ishuber.thrift.message
namespace py message.api
service MessageService {
bool sendMobileMessage(1:string mobile,2:string message);
bool sendEmailMessage(1:string email, 2:string message);
}
保存退出 :wq
2、生成python文件
thrift -gen py message.thrift
3、编辑python执行文件
cd gen-py
vi message_service.py
注:文件内容
from message.api import MessageService //报错将文件夹中messageService改为MessageService
from thrift.transport import TSocket //报错则将安装python中的thrift,使用easy_install thrift 进行安装
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
class MessageServiceHandler:
def sendMobileMessage(self,mobile,message):
print "sendMobileMessage"
return True
def sendEmailMessage(self,email,message):
print "sendEmailMessage"
return True
if __name__ =='__main__':
handler = MessageServiceHandler()
processor = MessageService.Processor(handler)
transport = TSocket.TServerSocket("localhost","9090")
tfactory = TTransport.TFramedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor,transport,tfactory,pfactory)
print "python thrift server start"
server.serve()
print "python thrift server exit"
保存退出 :wq
4、执行python文件,启动服务
python message_service.py
网友评论