gpt4 book ai didi

java - Apache Thrift Python-Java 'Connection Refused'

转载 作者:可可西里 更新时间:2023-11-01 14:04:31 25 4
gpt4 key购买 nike

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

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

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

这是怎么回事? (最近我还发现了一个解决这个问题的问题 https://issues.apache.org/jira/browse/THRIFT-1888 )

附言。使用 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()

最佳答案

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

关于java - Apache Thrift Python-Java 'Connection Refused',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16760741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com