import socket def query_model_server(host: str, port: int, instruction: str, buffer_size: int = 4096): """ 连接远程模型服务器,发送自然语言任务指令,获取返回的动作序列。 :param host: 模型服务器 IP :param port: 模型服务器端口 :param instruction: 要发送给服务器的任务描述 :param buffer_size: 接收缓冲区大小 :return: 服务端返回的动作字符串 """ try: # 创建 socket with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print(f"🔌 正在连接模型服务器 {host}:{port} ...") s.connect((host, port)) # 发送指令 s.sendall(instruction.encode('utf-8')) print(f"📨 已发送指令: {instruction}") # 接收服务器响应 data = s.recv(buffer_size) result = data.decode('utf-8') print("📥 服务器返回动作:", result) return result except Exception as e: print(f"❌ 客户端通信异常: {e}") return None if __name__ == "__main__": host = "10.103.69.239" port = 9999 instruction = "pick up the cup" action_result = query_model_server(host, port, instruction)