使用socket模块实现简单群聊

 1 import socket,threading,time,logging
 2 
 3 logging.basicConfig(level=logging.INFO,format="%(message)s")
 4 
 5 class Server:
 6 
 7     def __init__(self,ip="127.0.0.1",port=8080):
 8         self.person = {}
 9         self.ip = ip
10         self.port = port
11         self.socket = socket.socket()
12         self.event = threading.Event()
13 
14     def start(self):
15         self.socket.bind((self.ip,self.port))
16         self.socket.listen()
17         logging.info("server is start at {}".format(time.ctime()))
18         threading.Thread(target=self._accept).start()
19 
20     def stop(self):
21         for _,client in self.person.items():
22             client.close()
23         self.socket.close()
24         self.event.set()
25 
26     def _accept(self):
27         while not self.event.is_set():
28             client,addr = self.socket.accept()
29             self.person[addr] = client
30             logging.info("{} is connect on server".format(addr))
31             threading.Thread(target=self._rec,args=(client,addr,)).start()
32 
33     def _rec(self,client:socket.socket,addr):
34         while not self.event.is_set():
35             try:
36                 # 这里如果不进行异常处理,当一个客户端断开以后,其他客户端可能会受到影响,同时,在windows下可能因为,
37                 # 客户端强行端开后,第二次连接不能再使用recv()方法
38                 msg = client.recv(1024)
39                 msg = msg.decode("gbk")
40             except Exception as e:
41                 logging.info(e)
42                 msg = "quit"
43 
44             if msg == "quit":
45                 client.close()
46                 break
47             logging.info("{}发送的群消息是:{},发送的时间是:{}".format(addr,msg,time.ctime()))
48             self._broadcast(addr,msg.encode("gbk"))
49 
50     def _broadcast(self,addr,msg):
51         for caddr,client in self.person.items():
52             if caddr is not addr:
53                 client.send(msg)
54 
55 
56 c = Server()
57 c.start()
View Code

 

注意:主要实现了server端,客户端是用软件模拟的

  客户端软件下载地址:http://www.onlinedown.net/soft/43942.htm