【问题标题】:Apache Thrift Python-Java 'Connection Refused'Apache Thrift Python-Java '连接被拒绝'
【发布时间】:2013-05-26 15:29:39
【问题描述】:

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

我在 Python (PyPy) 上编写了一个服务器。我还编写了一个可以工作的参考客户端。

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

这有什么问题? (最近我也发现了一个封闭的issue,这个问题https://issues.apache.org/jira/browse/THRIFT-1888

PS。使用 Thrift 0.9 版本、PyPy 2.0 beta 2、Java 1.7.0_11

test.thrift

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

【问题讨论】:

    标签: java python windows networking thrift


    【解决方案1】:

    我有同样的问题。 用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);

    【讨论】:

      【解决方案2】:
      transport = new TSocket("localhost", 9091);
      TProtocol protocol = new TBinaryProtocol(transport);
      transport.open();
      
      This should work...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-01
        • 2018-02-11
        • 2011-08-07
        • 2020-10-01
        • 2016-11-11
        • 1970-01-01
        • 1970-01-01
        • 2017-08-08
        相关资源
        最近更新 更多