gpt4 book ai didi

python - python 中的 Thrift,简单示例的客户端无法连接到 Ubuntu 14.04 LTS 上的本地主机

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:12 24 4
gpt4 key购买 nike

我正在尝试通过一个简单的示例来了解 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:连接到本地主机端口 9090 (tcp) 失败:连接被拒绝

此时我不确定是哪个部分(计算机本身、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 .对于任何接口(interface),这应该是 0.0.0.0,对于 localhost,这应该是 127.0.0.1

编辑:

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

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

上述修复应该可以解决问题。

关于python - python 中的 Thrift,简单示例的客户端无法连接到 Ubuntu 14.04 LTS 上的本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006265/

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