1 import socket
2 from threading import Thread
3
4 """
5 服务端
6 1.要有固定的IP和PORT
7 2.24小时不间断提供服务
8 3.能够支持并发
9 """
10
11 server = socket.socket()
12 server.bind(('127.0.0.1',8080))
13 server.listen(5)
14
15
16 def talk(conn):
17 while True:
18 try:
19 data = conn.recv(1024)
20 if len(data) == 0:break
21 print(data.decode('utf-8'))
22 conn.send(data.upper())
23 except ConnectionResetError as e:
24 print(e)
25 break
26 conn.close()
27
28 while True:
29 conn, addr = server.accept() # 监听 等待客户端的连接 阻塞态
30 print(addr)
31 t = Thread(target=talk,args=(conn,))
32 t.start()