我最近尝试使用 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()