用thrift搭一个单进程服务

首先写一个服务的功能类,例如print 一些东西

HelloHandler.py

#!/bin/python
#encoding=utf8

import os
import sys
import json
import hashlib
import logging
import time
import random
import traceback
import signal

import re
from datetime import datetime

cur_dir = os.path.dirname(os.path.abspath(__file__))

#加载thrift接口的路径
thrift_path = os.path.sep.join([cur_dir, 'utils', 'thrift', 'gen-py'])
sys.path.append(thrift_path)


class HelloHandler():
    def __init__(self):
        self.name = None


    def HelloWorld(self,name):
        self.name = name
        print name
        return name

    
if __name__ == '__main__':
    name = "haha"
    tt_obj = HelloHandler()
    tt_obj.HelloWorld(name)
time.sleep(3)

 

服务端程序:Hello_server.py

 

#!/bin/python
#encoding=utf8

import os
import sys
import json
import hashlib
import logging
import pymongo
import time
import traceback
import signal
import urllib
import urllib2
import mimetypes
import re
import ConfigParser
from datetime import datetime
from bson import json_util
from collections import defaultdict
from multiprocessing import Process

sys.path.append("../utils/thrift/gen-py")
from HelloHandler import HelloHandler
from IE_service import IEService

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 HelloHandler

cur_dir = os.path.dirname(os.path.abspath(__file__))
cf = ConfigParser.ConfigParser()
conf_fn = os.path.sep.join([cur_dir, '../conf', 'thrift.conf'])
port = cf.read(conf_fn)
port = int(cf.get('thrift', 'port')) #这里设置端口

if __name__ == '__main__':
    '''
                level=logging.INFO \
                level=logging.WARNING \
                filename=cur_dir+'/var/SF_server.log' \
    '''
    logging.basicConfig( \
                level=logging.WARNING \
                , format='%(thread)d %(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s' \
            )
    handler = HelloHandler()
    processor = IEService.Processor(handler)
    transport = TSocket.TServerSocket(port=port)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
    server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
    print 'starting server'
    server.serve()
    manager.join()
    print 'done'

 

IE_server.thrift

namespace py IE_service

service IEService

    string HelloWorld(1:string req_json);

}

 

客户端程序

Client.py

 

#!/bin/python
#encoding=utf8
import sys
sys.path.append('./gen-py')
from IE_service import *
from IE_service import IEService

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
import json
try:
    transport = TSocket.TSocket('localhost', 62127) #端口为服务端设置的端口
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = IEService.Client(protocol)
    transport.open()
    req_dic = {"req_type":"type_doc", \
               "file_link":"/home/op/app/Project/msword-parser-server/bin/data/K0001-A.docx",\
              }
    print 'start'
    res = client.HelloWorld(json.dumps(req_dic))
    print res
    transport.close()
except Thrift.TException as e:
    print 'exceptino'
    print e

 

转载于:https://www.cnblogs.com/caitian/p/9306269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值