Use 12-Thrift_Python based on Test

Thrift_Python/... use

Python / Node.js / Golang / Php ... are similar, can be completed and preparation of client service, where to Python example.

Thrift's Python side can either write to the server or write to the client. (Golang please refer to the previous article)

Server side

In order to be compatible with the JS side, we are here to take the requirements as the standard.

Requirements: (Otherwise JS cannot resolve)

Json Protocol Packaging Protocol

Http Transport communication

MulTIpleProtocol/Processer (not required)

Business code source

With other languages, use the thrift compilation tool to compile the xxx.thrift file into a xx.py file and install the thrift base python library via pip.

Command:. Thrift -o -out ./pyModule --gen py Robot.thrift, pip install thrift

2. Method # coding: utf-8importsys sys.path.append ( "./ pyModule") fromthrift.transporTImportTHttpServerfromthrift.protocolimportTJSONProtocolfromthrift.protocol.TMulTIplexedProtocolimportTMulTIplexedProtocolimportRobotimportRobot.AudioclassRobotAudioHandle: defTtsPlay (self, strTxt, nPlayPriority): "" "Parameters: - strTxt - nPlayPriority "" "print (" RobotAudioHandle: ", strTxt, nPlayPriority) passhandler = RobotAudioHandle () processor = Robot.Audio.Processor (handler) server = THttpServer.THttpServer (TMultiplexedProcessor (processor," Audio "), (" 127.0. 0.1 ", 9000), TJSONProtocol.TJSONProtocolFactory) print (" Server start ... ") server.serve ()

This code can be modeled after Golang's demo, almost the same.

Tucao: Python's package mechanism is really a pit! ! !

Client side

Don't say much, just look at the code~~

The Robot source code library uses the same server, the same method.

# Coding: utf-8importsys sys.path.append ( "./ pyModule") fromthrift.transport.THttpClientimportTHttpClientfromthrift.protocol.TJSONProtocolimportTJSONProtocolfromthrift.protocol.TMultiplexedProtocolimportTMultiplexedProtocolimportRobotimportRobot.AudioclassRobotProxy: defflush (self): sys.stdout.flush () def__init __ (self) : self.Robot_tans = Noneself.protocol = Noneself.Audio = Noneself.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 try ( "Audio Proxy error!"): self.Robot_tans.open () except: ( "! Robot_tans or protocol error") print print ( "Load RobotProxy Module ...") app = RobotProxy ()

I wrote a lot of server-side code before, and I was a bit annoyed. I don’t do much analysis here. Just look at the code.

to sum up

Python is very easy to use as a script, but it's really crazy when writing rigorous code. It's mad when it's a library that doesn't have a lot of documentation like Thrift.

The code of this article was learned through the Test project, and the road is long and sloppy.

This is the pit of Thrift, the documentation is too small.

Code for other languages ​​is omitted here. If you have any questions, please check the Test directory for reference.

Camera Lens

SHAOXING COLORBEE PLASTIC CO.,LTD , https://www.colorbeephoto.com