本文主要是介绍python部署thrift服务以及客户端,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一步:定义接口
文件名:parse.thrift
内容如下:
service Parse {string parseHtml2Xml(1:string html)}
第二步:在thrift接口所在目录执行thrift命令
# thrift --gen py parse.thrift
这步会在当前目录生成gen-py文件夹
如果是用php,则用如下命令:
服务端:thrift --gen php:server helloworld.thrift
客户端:thrift --gen php:client helloworld.thrift
如果是用java,则用如下命令:
Linux环境:thrift --gen java importservice.thrift
windows环境: thrift-0.9.3.exe --gen java importservice.thrift
要注意的是,服务端和客户端使用的thrift版本要保持一致
第三步: server服务的开发,文件名,server.py
import sys
sys.path.append("./gen-py/parse")from Parse import *
from ttypes import *
from thrift.Thrift import TType, TMessageType, TException
from thrift.Thrift import TProcessor
from thrift.transport import TSocket
from thrift.protocol import TBinaryProtocol, TProtocol
from thrift.server import TServer
import logging
from constants import *
可在服务中定义日志模块,记录日志信息
logging.basicConfig(filename='logger.log', level=logging.INFO, format='%(asctime)s %(filename)s : %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
真正的服务代码
class ParseHandler:def parseHtml2Xml(self, html):logging.info("start parse html")return "1234"
def run():#创建服务端handler = ParseHandler()processor = Processor(handler)#监听端口transport = TSocket.TServerSocket('localhost', 9234)#选择传输层tfactory = TTransport.TBufferedTransportFactory()#选择传输协议pfactory = TBinaryProtocol.TBinaryProtocolFactory()#创建服务端server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)server.setNumThreads(5)logging.info('start thrift serve in python')server.serve()logging.info('done!')if __name__ == '__main__':run()
第四步:启动python服务
# python server.py
第五步: client客户端的开发,客户端文件名为client.py
import sys
sys.path.append('./gen-py/parse')from Parse import *
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
import requeststry:transport = TSocket.TSocket('localhost', 9234)transport = TTransport.TBufferedTransport(transport)protocol = TBinaryProtocol.TBinaryProtocol(transport)client = Client(protocol)transport.open()print 'start'res = requests.get('http://baike.baidu.com/item/%E7%A2%A7%E6%A1%82%E5%9B%AD%E5%98%89%E8%AA%89/19775381')xml = client.parseHtml2Xml(res.content)print xml transport.close()
except Thrift.TException as e:print 'exceptino'print e
第六步: 启动客户端调用
# python client.py
下面是一个thrift文件的case
service ImportService {//单个导入string singleImport(1:string url, 2:list<string> importModule, 3:i64 authorId)//批量新增导入string batchCreateImport(1:list<string> urls, 2:list<string> importModule, 3:i64 authorId)//批量覆盖导入string batchCoverImport(1:list<string> urls, 2:list<string> importModule, 3:i64 authorId)//检查相似度string checkSimilarity(1:string url, 2:list<string> importModule)//检查词条状态string checkLemmaStatus(1:string url)//批量导入时获取导入进度string getImportProcess(1:i64 authorId)
}
在thrift中,i32表示整型int,i64表示长整型long
这篇关于python部署thrift服务以及客户端的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!