Implementation of an RPC example of win7 + thrift + python

One installation environment

1 python install thrift package

    pip install thrift

2 Download the win version of "thrift-0.11.0.exe"

    http://thrift.apache.org/download

3 Install pychar's thrift plug-in to improve the automatic completion of writing thrift

    https://plugins.jetbrains.com/plugin/7331-thrift-support

Copy the .jar file after decompressing the downloaded "intellij-thrift.zip" to the lib directory under the pycharm installation directory.

 

Two examples of implementing a hello

1 Define hello.thrift

 

2 Compile with thrift compiler (using Cygwin64 Terminal)

$ ./thrift-0.11.0.exe -r -gen py hello.thrift

This generated the py code, in "gen-py". This package can be used by server and client code written by myself later.

3 Building a python project

Use pycharm to create the project "thrift_hello" project, and then copy the software package generated in the previous step to this project directory. The project directory is as follows. Because the path of the package does not seem to support '-' in Python, it is necessary to rename "gen-py" to "gen_py".

4 Write client and server implementation code.

hello_server.py

 

from gen_py.hello import Hello
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
from thrift.transport import TSocket, TTransport

__HOST = '127.0.0.1'
__PORT = 9090


class hello_handle:
    def helloString(self, para):
        print(para)
        return "result:" + para
if __name__ == '__main__':
         # Create server
    handler = hello_handle()
    processor = Hello.Processor(handler)
         # Listening port
    transport = TSocket.TServerSocket(host=__HOST, port=__PORT)
         # Select the transport layer
    tfactory = TTransport.TBufferedTransportFactory()
         # Choose transfer protocol
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()
         # Create server
    server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
    server.setNumThreads(5)

    print('Starting the server')
    server.serve()
    print('done')

hello_client.py

 

from gen_py.hello import Hello


from thrift.Thrift import TException
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TProtocol
from thrift.transport import TSocket, TTransport

__HOST = '127.0.0.1'
__PORT = 9090

if __name__ == '__main__':
    try:
        print ('Client starts ...')

        transport = TSocket.TSocket(host=__HOST, port=__PORT)
        transport = TTransport.TBufferedTransport(transport)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        client =  Hello.Client(protocol)
        transport.open()
        result = client.helloString("hello")
        print(result)
        transport.close()

        print('done')
    except TException as e:
        print(e)

 

 

5 Run the code

1. Run hello_server first

2 The output of running hello_client:

 

 

Intelligent Recommendation

【Python】 THRIFT RPC interface test Python

Articles directory THRIFT RPC interface test python 1. Basic command 2. Thrift's Sever design 3. THRIFT's client design 3. Follow points between communication 4. Thrift basic grammar 1. Issues that ne...

Python-RPC: Thrift interface definition language IDL

The IDL Thrift can use the following syntax to define interfaces described. 1 basic types bool: Boolean value, true or false byte: 8-bit signed integer i16: 16-bit signed integer i32: 32-bit signed in...

Implement RPC calls between Java and Python in Thrift

Scenes Introduction to Thrift and examples of using Thrift to implement RPC in Java: In the above, I talked about using Thrift to implement remote procedure calls in Java. Implemented the method of ca...

A simple example of getting started with thrift to implement rpc remote calling

System: centos7 The Thrift compiler will generate service interface code for the server and stubs for the client based on the selected target language. The parameters can be basic types and structures...

More Recommendation

RPC frame thrift- implementation of Go and Java remote procedure call

Official website: https://thrift.apache.org/ 1 overview We replace him into the way you are easy to understand: 2 hierarchical analysis 2.1 Transport (Transport Layer) The transport layer provides a s...

Python use RPC example

The remote process calls RPC commonly used and distributed computing, corresponding to the Python library namerpyc; The upper section is the RPYC server, and the lower section is called the RPYC call;...

RPC framework Thrift-python server running mode source code analysis

Thrift's python server running mode currently only supports four types, TSimpleServer, TThreadedServer, TThreadPoolServer, TForkingServer, the following is the analysis 1.TSimpleServer Single-threaded...

Python thrift rpc TProcessPoolServer native multi-process service mode

In the past, the multi-process service mode was realized by the mode of restarting the process in each thread by multi-threading. I discovered today that the process pool model actually comes with it....

Getting started with Netty framework-Thrift implements rpc calls in java and python

premise Install and configure python, I installed version 2.7 Install python-Liao Xuefeng's official website pycharm-python development tool Start 1. Install python dependencies For the development of...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top