TSocket读取0字节Apache Thrift MongoDB

时间:2018-02-19 20:53:22

标签: python mongodb rpc thrift mongoengine

我正在使用RPC框架Apache Thrift和Python开发一个小应用程序。我想使用MongoDB。 我正在使用mongoengine连接器,我定义了thrift数据模型并从中生成代码

将标签存储在服务器的列表中时,一切正常。但是,在尝试将标记存储在数据库中时出现错误

这是我的代码

server.py

import os
import sys

from mongoengine import *

sys.path.append(os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'gen-py'))

from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

from tag import *


class Tag(Document):
      identifier = LongField()
      description = StringField()

class TagHandler(object):

    #def __init__(self):
        #self.tags = []

    def createTag(self, identifier, description):
    tag = Tag(identifier=identifier, description=description).save()
        #self.tags.append(tag)
        return tag


connect("tags", host="127.0.0.1")
handler = TagHandler()
processor = TagService.Processor(handler)
transport = TSocket.TServerSocket('127.0.0.1', 9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

print "Starting python server..."
server.serve()

tag.thrift

exception TagException {

    1: string Reason
}



service TagService {

    i64 createTag(1: i64 identifier, 2: string description) throws (1:TagException e)


}

client.py

host = '127.0.0.1'
port = 9090

import sys
import os

sys.path.append(os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'gen-py'))

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

from tag import *




transport = TSocket.TSocket( host , port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = TagService.Client(protocol)
transport.open()
result = client.createTag(12345, "dirty creation")    
print ("success")
transport.close()

执行此操作时,我

thrift.transport.TTransport.TTransportException: TSocket read 0 bytes

在客户端和

No handlers could be found for logger "thrift.server.TServer"

在服务器

任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:0)

你应该小心:

i64 createTag(1: i64 identifier, 2: string description) throws (1:TagException e)

应该更好:

void createTag(1: i64 identifier, 2: string description) throws (1:TagException e)

小心,你必须要好好,直到下周二。洛尔。