栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python部署thrift服务以及客户端

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 requests

try:
    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 importModule, 3:i64 authorId)

    //批量新增导入
    string batchCreateImport(1:list urls, 2:list importModule, 3:i64 authorId)

    //批量覆盖导入
    string batchCoverImport(1:list urls, 2:list importModule, 3:i64 authorId)

    //检查相似度
    string checkSimilarity(1:string url, 2:list importModule)

    //检查词条状态
    string checkLemmaStatus(1:string url)

    //批量导入时获取导入进度
    string getImportProcess(1:i64 authorId)
}

 

 

在thrift中,i32表示整型int,i64表示长整型long
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/834132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号