服务端
from multiprocessing import Process from threading import Thread import socket def lunch(conn): while True: res = conn.recv(1024) if len(res) == 0:break res = res.decode('utf-8') msg = f'{res}外卖已经发货了' print(msg) conn.send(msg.encode("utf-8")) conn.close() if __name__ == '__main__': s = socket.socket() s.bind(('127.0.0.1',8080)) s.listen(5) while True: conn,addr = s.accept() # t = Thread(target=lunch,args=(conn,)) t = Process(target=lunch,args=(conn,)) t.start()
客户端
import socket s = socket.socket() s.connect(('127.0.0.1',8080)) while True: msg = input("your name:") s.send(msg.encode('utf-8')) data = s.recv(1024) print(data.decode('utf-8'))