57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
![]() |
import sys
|
||
|
from PySide6.QtCore import QCoreApplication, QByteArray
|
||
|
from PySide6.QtNetwork import QTcpSocket
|
||
|
from PySide6.QtCore import Slot, QObject
|
||
|
|
||
|
|
||
|
class ApiClient(QObject):
|
||
|
def __init__(self, host, port):
|
||
|
super().__init__()
|
||
|
|
||
|
# 创建一个 TCP 客户端对象
|
||
|
self.socket = QTcpSocket(self)
|
||
|
self.host = host
|
||
|
self.port = port
|
||
|
|
||
|
# 连接信号
|
||
|
self.socket.connected.connect(self.on_connected)
|
||
|
self.socket.readyRead.connect(self.on_ready_read)
|
||
|
self.socket.disconnected.connect(self.on_disconnected)
|
||
|
|
||
|
# 连接到服务器
|
||
|
self.socket.connectToHost(self.host, self.port)
|
||
|
|
||
|
@Slot()
|
||
|
def on_connected(self):
|
||
|
print(f"Connected to {self.host}:{self.port}")
|
||
|
|
||
|
# 发送读取请求 (示例: 读取 'key1')
|
||
|
self.socket.write(b"READ key1")
|
||
|
self.socket.flush()
|
||
|
print("Sent: READ key1")
|
||
|
|
||
|
@Slot()
|
||
|
def on_ready_read(self):
|
||
|
data = self.socket.readAll() # 读取服务器返回的数据
|
||
|
print(f"Received from server: {data.data().decode()}")
|
||
|
|
||
|
@Slot()
|
||
|
def on_disconnected(self):
|
||
|
print("Disconnected from server.")
|
||
|
self.socket.close() # 关闭连接
|
||
|
|
||
|
def get_params(self, list_param_name):
|
||
|
#
|
||
|
print(list_param_name)
|
||
|
# 返回一个字典
|
||
|
return
|
||
|
|
||
|
def set_params(self, list_param_name, list_param_data):
|
||
|
print(list_param_name)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
app = QCoreApplication()
|
||
|
api = ApiClient("127.0.0.1", 1234)
|
||
|
|
||
|
sys.exit(app.exec())
|