| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import threading
- class AppendClient(threading.Thread):
- def __init__(self, USERLIST, USER, BUFSIZE, ADDR):
- threading.Thread.__init__(self)
- self.setDaemon(True)
- self.USERLIST = USERLIST
- self.USER = USER
- self.BUFSIZE = BUFSIZE
- self.ADDR = ADDR
- pass
- def __del__(self):
- if self.USER in self.USERLIST:
- self.USERLIST.remove(self.USER)
- self.USER.close()
- pass
- def run(self):
- # 添加用户
- self.USERLIST.append(self.USER)
- # 接收用户信息
- while True:
- try:
- msg = self.USER.recv(self.BUFSIZE)
- if not msg:
- self.USERLIST.remove(self.USER)
- self.USER.close()
- print('client exit 一,UserListLenght:', len(self.USERLIST))
- break
- # 去除自己,客户端大于1才转发
- if len(self.USERLIST) > 1:
- for c in self.USERLIST:
- if c != self.USER:
- print('转发')
- c.sendall(msg)
- except:
- self.USERLIST.remove(self.USER)
- self.USER.close()
- print('client exit 二,UserListLenght:', len(self.USERLIST))
- break
- pass
|