forked from jackfrued/Python-100-Days
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatserver.py
44 lines (36 loc) · 1.21 KB
/
chatserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from socket import socket
from threading import Thread
def main():
class ClientHandler(Thread):
def __init__(self, client):
super().__init__()
self._client = client
def run(self):
try:
while True:
try:
data = self._client.recv(1024)
if data.decode('utf-8') == 'byebye':
clients.remove(self._client)
self._client.close()
break
else:
for client in clients:
client.send(data)
except Exception as e:
print(e)
clients.remove(self._client)
break
except Exception as e:
print(e)
server = socket()
server.bind(('10.7.189.118', 12345))
server.listen(512)
clients = []
while True:
curr_client, addr = server.accept()
print(addr[0], '连接到服务器.')
clients.append(curr_client)
ClientHandler(curr_client).start()
if __name__ == '__main__':
main()