Use 12-Thrift_Python based on Test

Thrift with Python, along with other languages like Node.js, Golang, and PHP, follows a similar structure. It can be used to build both client and server services. In this article, we’ll focus on the Python example.

The Python side of Thrift can either act as a server or a client. For Golang, you can refer to previous articles for more details.

On the server side, we need to make sure it’s compatible with the JavaScript side. Therefore, we follow specific requirements to ensure that the JS client can correctly parse the data.

Requirements (essential for JS compatibility):

  • Use the JSON Protocol for data serialization
  • Use HTTP Transport for communication
  • Support MultiplexedProtocol/Processor (optional but recommended)

For business code, the approach is similar across different languages. You use the Thrift compiler to generate Python files from the .thrift file, and then install the Thrift Python library via pip.

Command: thrift -o ./pyModule --gen py Robot.thrift, followed by pip install thrift.

Here’s an example of a simple Thrift server in Python:

# 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 started...")
server.serve()

This code is very similar to the Golang version, just structured differently due to Python's syntax.

Tip: Python’s package management can be quite tricky—especially when dealing with paths and imports. Be careful with your environment setup!

On the client side, here’s how you can connect to the Thrift server:

# 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()

Writing server-side code in Python was quite tedious, but I won’t go into too much detail here. Just look at the code and see how it works.

In summary, Python is great for scripting, but writing robust, production-ready code can be frustrating, especially when working with libraries that lack good documentation, like Thrift.

The code in this article was based on a test project, and the process was a bit messy. However, it helped me understand Thrift better.

One big pitfall with Thrift is its limited documentation. If you’re struggling, check the Test directory for examples.

Code for other languages is not included here, but feel free to explore the repository if you have any questions.

Wireless Power Bank

Wireless Power Bank,Fast Charge Power Bank,Usb Battery Pack,10000mah Power Bank

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