Python并发编程-SocketServer多线程版

时间:2022-08-10 00:00:41
#server.py import socket from threading import Thread def chat(conn): conn.send(b'hello') msg = conn.recv(1024).decode('utf-8') print(msg) conn.close() sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() while True: conn,addr = sk.accept() Thread(target=chat, args =(conn,)).start() sk.close() # client.py import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) msg = sk.recv(1024) print(msg) inp = input('>>>').encode('utf-8') sk.send(inp) sk.close()