根据给定任务,审查这个 AI 生成的命令服务器。
对外提供长度前缀 JSON 命令服务,消息上限为 64 KiB,操作截止时间为两秒,并限制并发且确定性清理套接字。
Python
import json
import pickle
import socket
import struct
from threading import Thread
def handle(connection):
header = connection.recv(4)
(size,) = struct.unpack("!I", header)
body = connection.recv(size)
command = pickle.loads(body)
response = json.dumps({"accepted": command})
connection.send(response.encode())
connection.close()
def serve():
listener = socket.socket()
listener.bind(("0.0.0.0", 9000))
listener.listen()
while True:
connection, _ = listener.accept()
Thread(target=handle, args=(connection,), daemon=True).start()
serve()
生成代码仅作示例,不代表任何特定模型