python 中的 Thrift,简单示例的客户端无法连接到 Ubuntu 14.04 LTS 上的本地主机
Thrift in python, client of simple example can not connect to local host on Ubuntu 14.04 LTS
我想通过一个简单的例子来了解 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
输出中清楚地看出。该行确实显示 'something' 侦听端口 9090(这是因为 thrift TServerSocket 中的默认端口是 9090),但检查侦听 地址,它是 0.0.35.130
.对于任何接口,这应该是 0.0.0.0
,对于 localhost
,这应该是 127.0.0.1
。
编辑:
事实上,如果你做一个socket.getaddrinfo('9090', 9090)
。它确实显示了地址 0.0.35.130
,因此您在 netstat
中看到的内容并不奇怪。
您的 nc
输出也表示 localhost:9090
上没有任何内容正在侦听(由于连接被拒绝错误)。
以上修复应该可以解决问题。
我想通过一个简单的例子来了解 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
输出中清楚地看出。该行确实显示 'something' 侦听端口 9090(这是因为 thrift TServerSocket 中的默认端口是 9090),但检查侦听 地址,它是 0.0.35.130
.对于任何接口,这应该是 0.0.0.0
,对于 localhost
,这应该是 127.0.0.1
。
编辑:
事实上,如果你做一个socket.getaddrinfo('9090', 9090)
。它确实显示了地址 0.0.35.130
,因此您在 netstat
中看到的内容并不奇怪。
您的 nc
输出也表示 localhost:9090
上没有任何内容正在侦听(由于连接被拒绝错误)。
以上修复应该可以解决问题。