Use 12-Thrift_Python based on Test

Thrift can be used with Python, Node.js, Golang, PHP, and other languages. These implementations are quite similar, allowing you to create both client and server services. In this article, we’ll focus on the Python example.

On the Python side, Thrift can be used to build either a server or a client. For Golang, you can refer to previous articles for more details.

Server Side

To ensure compatibility with the JavaScript side, we will follow the requirements strictly. Otherwise, the JS code may not be able to parse the responses correctly.

Requirements (for JS compatibility):

  • Json Protocol as the packaging protocol
  • Http Transport for communication
  • MultiplexedProtocol/Processor (not mandatory)

Business Code Source

Similar to other languages, you can use the Thrift compiler to generate Python files from the xxx.thrift file, and install the Thrift base library via pip.

Command: thrift -o ./pyModule --gen py Robot.thrift, then run pip install thrift.

Here’s an example of a Python server implementation:

# coding: utf-8
import sys
sys.path.append("./pyModule")
from thrift.transport import THttpServer
from thrift.protocol import TJSONProtocol
from thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocol
import Robot
import Robot.Audio

class RobotAudioHandle:
    def TtsPlay(self, strTxt, nPlayPriority):
        print("RobotAudioHandle: ", strTxt, nPlayPriority)

handler = RobotAudioHandle()
processor = Robot.Audio.Processor(handler)
server = THttpServer.THttpServer(
    TMultiplexedProtocol(processor, "Audio"),
    ("127.0.0.1", 9000),
    TJSONProtocol.TJSONProtocolFactory()
)
print("Server start...")
server.serve()

This code is very similar to the Golang version, making it easy to model after other language examples.

Note: Python's package system is really a pain! It can be very frustrating when dealing with imports and paths.

Client Side

Let’s skip the explanation and look at the code directly.

The Robot source code uses the same server and methods as before.

# coding: utf-8
import sys
sys.path.append("./pyModule")
from thrift.transport.THttpClient import THttpClient
from thrift.protocol.TJSONProtocol import TJSONProtocol
from thrift.protocol.TMultiplexedProtocol import TMultiplexedProtocol
import Robot
import Robot.Audio

class RobotProxy:
    def flush(self):
        sys.stdout.flush()

    def __init__(self):
        self.Robot_tans = None
        self.protocol = None
        self.Audio = None
        self.Robot_tans = THttpClient("http://127.0.0.1:9000/robot")
        self.protocol = TJSONProtocol(self.Robot_tans)
        try:
            self.Audio = Robot.Audio.Client(TMultiplexedProtocol(self.protocol, "Audio"))
        except:
            print("Audio Proxy error!")
        try:
            self.Robot_tans.open()
        except:
            print("! Robot_tans or protocol error")
        print("Load RobotProxy Module...")
        
app = RobotProxy()

I wrote a lot of server-side code before, and it was quite frustrating. I won’t go into too much detail here—just take a look at the code.

Summary

Python is very easy to use as a scripting language, but it can be quite challenging when writing strict, production-level code. Especially when working with libraries like Thrift that have limited documentation, things can get really messy.

The code in this article was based on a test project, and the learning process was long and filled with trial and error.

One of the biggest pitfalls of Thrift is its lack of comprehensive documentation.

For other languages, the code is omitted here. If you have any questions, feel free to check the Test directory for reference.

TWS Earphone


TWS Earphone


True Wireless ( or TWS) headphones are the latest in amazing audio technology. From Apple Airpods to the TaoTronics SoundLiberty series, True Wireless Earbuds have moved to the forefront of audio technology. When you wear TWS earbuds, you have the complete freedom to move: there are no wires to connect your earbuds to your phone, or even to each other.It`s the best earphones.



TWS earphone7

TWS Earphone,Baseus Wireless Earphones,Tws Under 1000,Lenovo Ear Buds

Pogo Technology International Ltd , https://www.wisesir.net