Search code examples
pythonpython-2.7exceptionnetwork-programmingthrift

Thrift TTransportException in python


I'm getting a weird error while trying to execute an RPC using thrift on python. I have found similar issues online, but none of them really apply to my situation.

Here is the error I'm getting

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)]

The following is, I believe, the code which produces it.

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()

This happens whenever I try to communicate to another machine, so I tried with the ip "127.0.0.1" and it works. However, using the IP of the localhost "192.168.178.44" which should refer to the same computer also produces the error. To me it seems like it cannot resolve IP addresses for some reason... Any ideas on what's causing this and how to fix it?

I'm using Python 2.7.12, thrift 0.9.3 and Ubuntu 16.04, but I also got the error on Windows 10.


This is how my thrift service starts

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()

Solution

  • your server should bind to that address before client could connect to:

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

    or use host='0.0.0.0' which means bind on all IPv4 addresses on the machine.