【问题标题】:Thrift TTransportException in pythonpython中的Thrift TTransportException
【发布时间】:2017-09-24 13:32:11
【问题描述】:

尝试在 python 上使用 thrift 执行 RPC 时遇到一个奇怪的错误。我在网上发现了类似的问题,但没有一个真正适用于我的情况。

这是我遇到的错误

No handlers could be found for logger "thrift.transport.TSocket"
Traceback (most recent call last):
  File "experiment.py", line 71, in <module>
    transport.open()
  File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TTransport.py", line 152, in open
    return self.__trans.open()
  File "/usr/local/lib/python2.7/dist-packages/thrift/transport/TSocket.py", line 113, in open
    raise TTransportException(TTransportException.NOT_OPEN, msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('192.168.178.44', 9000)]

我相信以下是生成它的代码。

TECS_SERVER_IP = "192.168.178.44"
TECS_SERVER_PORT = 9000

transport = TSocket.TSocket(TECS_SERVER_IP, TECS_SERVER_PORT)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = TTSService.Client(protocol)
transport.open()

每当我尝试与另一台机器通信时都会发生这种情况,因此我尝试使用 ip“127.0.0.1”并且它可以工作。但是,使用应指同一台计算机的本地主机“192.168.178.44”的 IP 也会产生错误。 对我来说,它似乎由于某种原因无法解析 IP 地址...... 关于导致此问题的原因以及如何解决此问题的任何想法?

我使用的是 Python 2.7.12、thrift 0.9.3 和 Ubuntu 16.04,但我在 Windows 10 上也遇到了错误。


这就是我的节俭服务的启动方式

handler = TTSHandler()
handler.__init__()
transport = TSocket.TServerSocket(host='localhost', port=9000)
processor = TTSService.Processor(handler)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
server.serve()

【问题讨论】:

  • 你的 TServer 是如何启动的?
  • @georgexsh 检查我的新编辑

标签: python python-2.7 exception network-programming thrift


【解决方案1】:

您的服务器应该在客户端可以连接之前绑定到该地址:

TSocket.TServerSocket(host='192.168.178.44', port=9000)

或使用host='0.0.0.0',这意味着绑定机器上的所有 IPv4 地址。

【讨论】:

  • 所以它不应该是'localhost'?
  • 'localhost' 是一个特殊的主机名,解析为'127.0.0.1',如果服务器绑定,它只接受连接到'127.0.0.1'。
  • 似乎在本地工作。我明天远程试试。非常感谢!
猜你喜欢
  • 2014-11-03
  • 2014-08-04
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多