【问题标题】:python thrift error ```TSocket read 0 bytes```python节俭错误```TSocket读取0字节```
【发布时间】:2017-01-15 05:43:22
【问题描述】:

我的python版本:2.7.8
节俭版:0.9.2
python-thrift 版本:0.9.2
操作系统:centOS 6.8
我的 test.thrift 文件:

const string HELLO_IN_KOREAN = "an-nyoung-ha-se-yo"
const string HELLO_IN_FRENCH = "bonjour!"
const string HELLO_IN_JAPANESE = "konichiwa!"

service HelloWorld {
  void ping(),
  string sayHello(),
  string sayMsg(1:string msg)
}

client.py

# -*-coding:utf-8-*-

from test import HelloWorld
from test.constants import *

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


# Make socket
transport = TSocket.TSocket('192.168.189.156', 30303)

# Buffering is critical. Raw sockets are very slow
transport = TTransport.TBufferedTransport(transport)

# Wrap in a protocol
protocol = TBinaryProtocol.TBinaryProtocol(transport)

# Create a client to use the protocol encoder
client = HelloWorld.Client(protocol)

# Connect!
transport.open()

client.ping()
print "ping()"

msg = client.sayHello()
print msg
msg = client.sayMsg(HELLO_IN_KOREAN)
print msg

transport.close()

server.py:

# -*-coding:utf-8-*-

from test.HelloWorld import Processor
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer


class HelloWorldHandler(object):
    def __init__(self):
        self.log = {}

    def ping(self):
        print "ping()"

    def sayHello(self):
        print "sayHello()"
        return "say hello from 156"

    def sayMsg(self, msg):
        print "sayMsg(" + msg + ")"
        return "say " + msg + " from 156"


handler = HelloWorldHandler()
processor = Processor(handler)
transport = TSocket.TServerSocket("192.168.189.156", 30303)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)

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

我的错误:

ping()
Traceback (most recent call last):
  File "client.py", line 29, in <module>
    msg = client.sayHello()
  File "/home/zhihao/bfd_mf_report_warning_service/local_test/test/HelloWorld.py", line 68, in sayHello
    return self.recv_sayHello()
  File "/home/zhihao/bfd_mf_report_warning_service/local_test/test/HelloWorld.py", line 79, in recv_sayHello
    (fname, mtype, rseqid) = iprot.readMessageBegin()
  File "build/bdist.linux-x86_64/egg/thrift/protocol/TBinaryProtocol.py", line 126, in readMessageBegin
  File "build/bdist.linux-x86_64/egg/thrift/protocol/TBinaryProtocol.py", line 206, in readI32
  File "build/bdist.linux-x86_64/egg/thrift/transport/TTransport.py", line 58, in readAll
  File "build/bdist.linux-x86_64/egg/thrift/transport/TTransport.py", line 159, in read
  File "build/bdist.linux-x86_64/egg/thrift/transport/TSocket.py", line 120, in read
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes

【问题讨论】:

    标签: python thrift


    【解决方案1】:

    我猜这个问题很老了,但我遇到了同样的错误信息。 原来是服务器端有错别字。 thrift 库试图使用 Python 日志记录消息,但我没有设置日志记录,所以它只是说,“No handlers could be found for logger "thrift.server.TServer"”。

    当我做了一些最小的日志记录时,(将此代码添加到服务器端):

    import logging
    logging.basicConfig(level=logging.DEBUG)
    

    日志记录显示了我在 Python 堆栈跟踪中的拼写错误,我修复了它并且它再次工作。 “TSocket read 0 bytes”错误意味着服务器出现异常并且没有写出消息。

    【讨论】:

      【解决方案2】:

      我的操作系统环境问题。
      我将端口30303改为9999,运行成功。

      【讨论】:

      • 事实上,我确实在您的代码中发现了任何错误。我让 thrift 服务器监听 30303 端口,没有任何错误。
      【解决方案3】:

      启动 thrift 服务时,需要设置协议,如下所示:

      ./hbase-daemon.sh start thrift -c compact protocol;
      

      在您的代码中,您需要设置protocol = 'compact',如下所示:

      con = happybase.Connection(host='localhost',port=your thriftport,autoconnect=True,protocol = 'compact',timeout = xxx)
      

      【讨论】:

        【解决方案4】:

        我遇到了这个问题,因为我的实现引发了一个未指定服务端点引发的异常,我通过在我的端点规范的末尾添加 throws ( 1: MyException exc) 来修复它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-10-08
          • 2019-04-23
          • 1970-01-01
          • 2014-04-10
          • 2017-12-31
          • 2011-05-03
          • 2016-05-15
          相关资源
          最近更新 更多