猿问

Apache Thrift Python-Java“连接被拒绝”

我最近尝试使用Thrift将Python连接到Java。


我已经在Python(PyPy)上编写了服务器。我还写了一个有效的参考客户。


然后,我编写了一个Java客户端,该客户端仅产生“拒绝连接”异常。


这怎么了 (最近我还发现了一个封闭的问题,该问题的特征是https://issues.apache.org/jira/browse/THRIFT-1888)


PS。使用过的Thrift 0.9版本,PyPy 2.0 beta 2,Java 1.7.0_11


节俭


namespace java com.test

namespace python test


service TestPing {

   void ping()

Python服务器代码


class TestPingHandler:

  def ping(self):

    pass


handler = TestPingHandler()

processor = TestPing.Processor(handler)

transport = TSocket.TServerSocket(port=9091)

tfactory = TTransport.TBufferedTransportFactory()

pfactory = TBinaryProtocol.TBinaryProtocolFactory()


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


print 'Starting the server...' 

server.serve()

print 'done.' 

Java客户端代码


TTransport transport;

transport = new TSocket("localhost", 9091);

transport.open();

TProtocol protocol = new TBinaryProtocol(transport);

client = new TestPing.Client(protocol);

client.ping();

参考Python客户端代码


transport = TSocket.TSocket('localhost', 9091)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = TestPing.Client(protocol)

transport.open()

client.ping()

transport.close()


德玛西亚99
浏览 240回答 2
2回答

慕尼黑8549860

我遇到过同样的问题。用ip替换“ localhost”来修复它。原因是:Python使用TCPV6,而Java使用TCP。Python: transport = TSocket.TServerSocket(host="127.0.0.1", port = 9091)Java的: transport = new TSocket("127.0.0.1", 9091);

jeck猫

transport = new TSocket("localhost", 9091);TProtocol protocol = new TBinaryProtocol(transport);transport.open();This should work...
随时随地看视频慕课网APP

相关分类

Python
我要回答