检查连接是否关闭

2024-06-26 03:47:03 发布

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

我用的是cherrypy和ws4py。在

如果浏览器切断了websocket连接,def received_message(self, m):中的任何写入操作都会引发错误

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/Library/Python/2.7/site-packages/ws4py/manager.py", line 310, in run
    if not ws.once():
  File "/Library/Python/2.7/site-packages/ws4py/websocket.py", line 388, in once
    if not self.process(b):
  File "/Library/Python/2.7/site-packages/ws4py/websocket.py", line 445, in process
    self.close(s.closing.code, s.closing.reason)
  File "/Library/Python/2.7/site-packages/ws4py/websocket.py", line 189, in close
    self._write(self.stream.close(code=code, reason=reason).single(mask=self.stream.always_mask))
  File "/Library/Python/2.7/site-packages/ws4py/websocket.py", line 279, in _write
    self.sock.sendall(b)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 32] Broken pipe

如何判断连接是否关闭?在


Tags: inpyselfclosepackageslinelibrarysite