AppendClient.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import threading
  2. class AppendClient(threading.Thread):
  3. def __init__(self, USERLIST, USER, BUFSIZE, ADDR):
  4. threading.Thread.__init__(self)
  5. self.setDaemon(True)
  6. self.USERLIST = USERLIST
  7. self.USER = USER
  8. self.BUFSIZE = BUFSIZE
  9. self.ADDR = ADDR
  10. pass
  11. def __del__(self):
  12. if self.USER in self.USERLIST:
  13. self.USERLIST.remove(self.USER)
  14. self.USER.close()
  15. pass
  16. def run(self):
  17. # 添加用户
  18. self.USERLIST.append(self.USER)
  19. # 接收用户信息
  20. while True:
  21. try:
  22. msg = self.USER.recv(self.BUFSIZE)
  23. if not msg:
  24. self.USERLIST.remove(self.USER)
  25. self.USER.close()
  26. print('client exit 一,UserListLenght:', len(self.USERLIST))
  27. break
  28. # 去除自己,客户端大于1才转发
  29. if len(self.USERLIST) > 1:
  30. for c in self.USERLIST:
  31. if c != self.USER:
  32. print('转发')
  33. c.sendall(msg)
  34. except:
  35. self.USERLIST.remove(self.USER)
  36. self.USER.close()
  37. print('client exit 二,UserListLenght:', len(self.USERLIST))
  38. break
  39. pass