from py.thrift.generated import PersonService from py.thrift.generated import ttypes from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TCompactProtocol try: tSocket = TSocket.TSocket("localhost", 8899) tSocket.setTimeout(900) transport = TTransport.TFramedTransport(tSocket) protocol = TCompactProtocol.TCompactProtocol(transport) client = PersonService.Client(protocol) transport.open() person = client.getPersonByUsername("张三") print(person.username) print(person.age) print(person.married) print('---------------------') newPerson = ttypes.Person() newPerson.username = "******" newPerson.age = 30 newPerson.married = True
# coding=utf-8 from py.thrift.generated import PersonService from PersonServiceImpl import PersonServiceImpl from thrift import Thrift from thrift.server import TNonblockingServer from thrift.server import TServer from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TCompactProtocol try: personServerHandler = PersonServiceImpl() processer = PersonService.Processor(personServerHandler) socket = TSocket.TServerSocket(host='127.0.0.1', port=8899) transportFactory = TTransport.TFramedTransportFactory() protocalFactory = TCompactProtocol.TCompactProtocolFactory() server = TServer.TSimpleServer(processer, socket, transportFactory, protocalFactory) server.serve() except Thrift.TException as tx: print('----')
from PersonServiceImpl import PersonServiceImpl from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TCompactProtocol from thrift.server import TServer import sys #修改字符编码 解决thrift无法传输中文问题 reload(sys) sys.setdefaultencoding('utf-8') try: personServiceHandler = PersonServiceImpl() #设置Processor processor = PersonService.Processor(personServiceHandler) #绑定端口号 serverSocket = TSocket.TServerSocket(port=8899) #服务端使用Factory 客户端使用Transport transportFactory = TTransport.TFramedTransportFactory() #服务端使用Factory 客户端使用Protocol protocolFactory = TCompactProtocol.TCompactProtocolFactory() server = TServer.TSimpleServer(processor, serverSocket, transportFactory, protocolFactory) server.serve() except Thrift.TException, ex: print '%s' % ex.message
from py.thrift.generated import PersonService, ttypes from thrift import Thrift from PersonServerImpl import PersonServerImpl from thrift.transport import TTransport from thrift.transport import TSocket from thrift.protocol import TCompactProtocol from thrift.server import TServer import sys reload(sys) sys.setdefaultencoding('utf-8') try: PersonServerHandler = PersonServerImpl() # 处理器 processor = PersonService.Processor() # 传输通道 serverSocket = TSocket.TServerSocket(port=8899) # 传输格式 transportFactory = TTransport.TFramedTransportFactory() # 传输协议 protocolFactory = TCompactProtocol.TCompactProtocolFactory() # 传输模型 server = TServer.TSimpleServer(processor, serverSocket, transportFactory, protocolFactory) server.serve() except Thrift.TException, ex: print "%s" % ex.message