Python RPC协议调用

# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: [email protected]
@file: server.py
@time: 2023/7/29 21:00
@desc: 服务端代码

"""
from xmlrpc.server import SimpleXMLRPCServer

class Test():

    def test(self):
        print("rpc-test")
        return "rpc-test"

    def name(self):
        print("rpc-name")
        return "rpc-name"


if __name__ == '__main__':
    server = SimpleXMLRPCServer(("服务器ip",服务器端口),allow_none=True)
    # 注册Test()实例
    server.register_instance(Test(),allow_dotted_names=True)
    # 启动服务
    server.serve_forever()
# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: [email protected]
@file: client.py
@time: 2023/7/29 20:54
@desc: 客户端

"""
from xmlrpc.client import ServerProxy

if __name__ == '__main__':
    client = ServerProxy("http://服务器ip:服务器端口")
    # 调用服务器中注册的实例方法
    print(client.test())
    print(client.name())

你可能感兴趣的:(python,rpc,开发语言)