Python uses Thrift

 

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u013474436/article/details/97782226

0x00

First you need to install python package of thrift

sudo pip install thrift
  • 1

0x01

Then write a simple IDL filehelloworld.thrift

const string HELLO_WORLD = "world"

service HelloWorld {
    void ping(), string sayHello(), string sayMsg(1:string msg) } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

thrift python scripting language code developed by Thrift editor generates required. which is:

thrift -r --gen py helloworld.thrift 
  • 1

Generation gen-py catalog:

├── gen-py
│   ├── helloworld
│   │   ├── constants.py
│   │   ├── HelloWorld.py
│   │   ├── HelloWorld-remote
│   │   ├── __init__.py
│   │   └── ttypes.py
│   └── __init__.py
└── helloworld.thrift
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

0x02

Thrift is a typical CS structure, the client and server can use different language development. In this paper, python, for example:
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!" 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

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, tx: print "%s" % (tx.message) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

Current directory structure:

├── gen-py
├── helloworld.thrift
├── PythonClient.py
└── PythonServer.py
  • 1
  • 2
  • 3
  • 4

0x03

Run server-side:

$ python PythonServer.py 
Starting python server...
  • 1
  • 2

Run client side:

$ python PythonClient.py 
ping()
say hello from 10.27.73.176
say world from 10.27.73.176
  • 1
  • 2
  • 3
  • 4

Output server side:

ping()
sayHello()
sayMsg(world)
  • 1
  • 2
  • 3

0x04 use thriftpy

thriftpy (now updated thriftpy2) thrift of the package, dynamic analysis can thrift interface file. Project Address: https://github.com/Thriftpy/thriftpy2

Installation thriftpy2

sudo pip install thriftpy2
  • 1

Write IDL

pingpong.thrift

service PingService {
    string ping(), } service AargsPingService { string ping(1:string ping); } service Sleep { oneway void sleep(1: i32 seconds) } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Write code

server.py

# coding=utf-8
import thriftpy2
from thriftpy2.rpc import make_server pp_thrift = thriftpy2.load("pingpong.thrift", module_name="pp_thrift") # 实现.thrift文件定义的接口 class Dispatcher(object): def ping(self): print("ping pong!") return 'pong' def main(): # 定义监听的端口和服务 server = make_server(pp_thrift.PingService, Dispatcher(), '127.0.0.1', 6000) print("serving...") server.serve() if __name__ == '__main__': main() 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

client.py

# coding=utf-8
import thriftpy2
#from thriftpy2.rpc import client_context
from thriftpy2.rpc import make_client # 读入thrift文件,module_name最好与server端保持一致,也可以不保持一致 pp_thrift = thriftpy2.load("pingpong.thrift", module_name="pp_thrift") def main(): #with client_context(pp_thrift.PingService, '127.0.0.1', 6000) as c: # pong = c.ping() # print(pong) client = make_client(pp_thrift.PingService, '127.0.0.1', 6000) print(client.ping()) if __name__ == '__main__': main() 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Notes to create a link in another way.

Directory Structure:

├── client.py
├── pingpong.thrift
└── server.py
  • 1
  • 2
  • 3

run

Run server-side:

$ python server.py 
serving...
  • 1
  • 2

Run client side:

$ python client.py 
pong
  • 1
  • 2

Output server side:

ping pong!

Guess you like

Origin www.cnblogs.com/fengff/p/11492209.html