55 lines
1.2 KiB
Python
55 lines
1.2 KiB
Python
|
"""
|
|||
|
@Time : 2022/9/30 17:09
|
|||
|
@Auth : 东
|
|||
|
@File :RpcClient.py
|
|||
|
@IDE :PyCharm
|
|||
|
@Motto:ABC(Always Be Coding)
|
|||
|
@Desc:RPC客户端
|
|||
|
|
|||
|
"""
|
|||
|
|
|||
|
import json
|
|||
|
import socket
|
|||
|
import time
|
|||
|
|
|||
|
|
|||
|
class TCPClient(object):
|
|||
|
def __init__(self):
|
|||
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|||
|
|
|||
|
def connect(self, host, port):
|
|||
|
"""链接Server端"""
|
|||
|
self.sock.connect((host, port))
|
|||
|
|
|||
|
def send(self, data):
|
|||
|
"""将数据发送到Server端"""
|
|||
|
self.sock.send(data)
|
|||
|
|
|||
|
def recv(self, length):
|
|||
|
"""接受Server端回传的数据"""
|
|||
|
return self.sock.recv(length)
|
|||
|
|
|||
|
|
|||
|
class RPCStub(object):
|
|||
|
|
|||
|
def __getattr__(self, function):
|
|||
|
def _func(*args, **kwargs):
|
|||
|
d = {'method_name': function, 'method_args': args, 'method_kwargs': kwargs}
|
|||
|
self.send(json.dumps(d).encode('utf-8')) # 发送数据
|
|||
|
data = self.recv(1024) # 接收方法执行后返回的结果
|
|||
|
return data.decode('utf-8')
|
|||
|
|
|||
|
setattr(self, function, _func)
|
|||
|
return _func
|
|||
|
|
|||
|
|
|||
|
class RPCClient(TCPClient, RPCStub):
|
|||
|
pass
|
|||
|
|
|||
|
|
|||
|
# c = RPCClient()
|
|||
|
# c.connect('127.0.0.1', 5003)
|
|||
|
# print(c.add(1, 2, 3))
|
|||
|
# print(c.setData({"sss": "ssss", "list": [5, 2, 3, 4]}))
|
|||
|
|