import threading import time class ComCationServer(threading.Thread): def __init__(self, client, userList): threading.Thread.__init__(self) self.setDaemon(True) self.client = client self.userList = userList def __del__(self): if self.client in self.userList: self.userList.remove(self.client) self.client.close() def run(self): db = str.encode('服务器收到') while True: try: msg = self.client.recv(1024) if not msg: self.userList.remove(self.client) self.client.close() print('clinet exit1:', len(self.userList)) break # 去除自己,客户端大于1才转发 for c in self.userList: if c != self.client: # print('转发') c.sendall(msg) # self.client.sendall(db) print('客户信息', msg, time.ctime()) time.sleep(1) except: self.userList.remove(self.client) self.client.close() print('client exit2:', len(self.userList)) break