如何修复代码中的“BrokenPipeError”?

2024-04-25 09:02:34 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在处理一些我发现的python chat room code,我把它配置成运行没有问题,除了当客户机向服务器发送消息时,服务器不会向每个客户机发送消息,因为广播代码中有一个BrokenPipeError。我在这里查看了一些关于堆栈溢出的其他问题,但我似乎无法找出错误发生的确切原因以及如何修复它

以下是客户端发送消息时服务器终端上显示的内容:

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "WMserver.py", line 24, in handle_client
    broadcast(bytes(msg, "utf8"))
  File "WMserver.py", line 43, in broadcast
    sock.send(bytes(prefix, "utf8")+msg)
BrokenPipeError: [Errno 32] Broken pipe

所指的代码是:

def broadcast(msg, prefix=""):  # prefix is for name identification.
    """Broadcasts a message to all the clients."""

    for sock in clients:
        sock.send(bytes(prefix, "utf8")+msg)

sock.send()中似乎有某种问题,但我不知道它可能是什么


Tags: inpyself服务器send消息客户机prefix