环境及版本
1 2 3 4 5
| 【Server、Client】 Ubuntu 14.04 LTS x64 Thriftpy 0.3.8 Python 2.7 Pycharm 4.5.1
|
简单Server & Client
Thriftpy的使用和Thrift类似,用两台Ubuntu分别做Server和Client,实现跨机器通信
Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
import thriftpy from thriftpy.rpc import make_server import os
class MyRPC(object): def print_fun(self,name): str = "Hello " + name return str
if __name__ == "__main__": file_path = os.path.abspath("../conf/simple.thrift") simple_thrift = thriftpy.load(file_path, module_name="simple_thrift")
server = make_server(simple_thrift.RPCTest, MyRPC(), '192.168.1.105', 6000) print "Thriftpy listening 6000......" server.serve()
|
Client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
import thriftpy from thriftpy.rpc import make_client import os
if __name__ == "__main__": file_path = os.path.abspath("../conf/simple.thrift")
simple_thrift = thriftpy.load(file_path, module_name="simple_thrift")
client = make_client(simple_thrift.RPCTest, '192.168.1.105', 6000) print client.print_fun("wxmimperio")
|
Service Conf
方法注册文件以.thrif为后缀,Server与Client都必须有,文件目录可以指定
1 2 3
| service RPCTest { string print_fun(1:string name), }
|
Thrift支持的数据类型Thriftpy里都支持