Python Thrift通信无BUG版

1、安装Thrift包

sudo pip install thrift

2、下载thrift.exe
官网 上下载 windows 版的 thrift.exe:(我这里用的是0.9.3版本)
在这里插入图片描述3、将thrift-0.9.3.exe改名为thrift.exe
4、编写一个简单接IDL文件helloworld.thrift

const string HELLO_WORLD = "world"

service HelloWorld {
    void ping(),
    string sayHello(),
    string sayMsg(1:string msg)
}

5、执行thrift.exe(打开命令窗口将路径切换到thrift.exe和helloworld.thrift所在路径下在执行下述命令)

thrift -r --gen py helloworld.thrift 

执行完成后生成gen-py文件夹在这里插入图片描述
6、在工程目录thrift下新建一个PythonServer.py(开头部分红色报错没关系,如图)
在这里插入图片描述

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(socket.gethostname())

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


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

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

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

7、在工程目录thrift下新建一个PythonClient.py(开头部分红色报错没关系,如图)
在这里插入图片描述

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('127.0.0.1', 30303)

    # 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_WORLD")
    print(msg)
    
    transport.close()

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

8、运行PythonServer,报错。
在这里插入图片描述
点击红色箭头处,加了个.
在这里插入图片描述在这里插入图片描述
再次运行:
在这里插入图片描述
9、运行PythonClient,报错。
在这里插入图片描述点击红色箭头处,加了个.
在这里插入图片描述
在这里插入图片描述
再次运行,成功!!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AIOT魔法师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值