我正在尝试通过一个简单的示例来学习Thrift(0.9.2版)在python中的工作方式。服务器代码工作正常,但是运行客户端代码会给出错误Could not connect to localhost:9090,我尝试了shell命令

netstat -nap | grep 9090,此输出

tcp 0 0 0.0.35.130:9090 0.0.0.0:* LISTEN 4656/python

和命令

nc -zv localhost 9090输出

nc: connect to localhost port 9090 (tcp) failed: Connection refused

在这一点上,我不确定哪一部分(计算机本身,Thrift或代码?)出了问题。所有的代码如下,有人会指出错误在哪里吗?

以下是helloworld.thrift

const string HELLO_IN_KOREAN = "an-nyoung-ha-se-yo"
const string HELLO_IN_FRENCH = "bonjour!"
const string HELLO_IN_JAPANESE = "konichiwa!"
service HelloWorld {
  void ping(),
  i32 sayHello(),
  i32 sayMsg(1:string msg)
}


服务器代码是

import sys
sys.path.append('../gen-py')

from helloworld import HelloWorld
from helloworld.ttypes import *

from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer

import socket

class HelloWorldHandler:
  def __init__(self):
    self.log = {}

  def ping(self):
    print "ping()"

  def sayHello(self):
    print "sayHello()"
    return "say hello from " + socket.gethostbyname()

  def sayMsg(self, msg):
    print "sayMsg(" + msg + ")"
    return "say " + msg + " from " + socket.gethostbyname()

handler = HelloWorldHandler()
processor = HelloWorld.Processor(handler)
transport = TSocket.TServerSocket('9090')
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()

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

print "Starting python server..."
server.serve()
print "done!"


这是客户,

import sys
sys.path.append('../gen-py')

from helloworld import HelloWorld
from helloworld.ttypes import *
from helloworld.constants import *

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

try:
  # Make socket
  transport = TSocket.TSocket('localhost', 9090)

  # Buffering is critical. Raw sockets are very slow
  transport = TTransport.TBufferedTransport(transport)

  # Wrap in a protocol
  protocol = TBinaryProtocol.TBinaryProtocol(transport)

  # Create a client to use the protocol encoder
  client = HelloWorld.Client(protocol)

  # Connect!
  transport.open()

  client.ping()
  print "ping()"

  msg = client.sayHello()
  print msg
  msg = client.sayMsg(HELLO_IN_KOREAN)
  print msg

  transport.close()

except Thrift.TException, tx:
  print "%s" % (tx.message)

最佳答案

我认为问题出在您的电话上-

transport = TSocket.TServerSocket('9090')


这应该是

transport = TSocket.TServerSocket(host='localhost', port=9090)


要么

transport = TSocket.TServerSocket(port=9090)


实际上,甚至不需要port参数。如果您不提供,则默认值为9090。您的代码表示主机为9090

这可以从netstat -nap输出中清除。该行的确显示了正在侦听端口9090的“某物”(这是因为thrift TServerSocket中的默认端口是9090),但是检查了侦听地址,它是0.0.35.130。对于任何接口,此名称应为0.0.0.0;对于127.0.0.1,此名称应为localhost

编辑:

实际上,如果您执行socket.getaddrinfo('9090', 9090)。它确实显示了0.0.35.130的地址,因此在netstat中看到的内容并不奇怪。

您的nc输出还表示localhost:9090上没有监听任何内容(通过“连接被拒绝”错误)。

上面的修复程序应该可以解决问题。

07-24 13:58