server端:
import socket server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) server.bind(('127.0.0.1',8080)) server.listen(5) while 1: conn,client_addr = server.accept() print(client_addr) while 1: try: data = conn.recv(1024) if not data: continue print(data) conn.send(data.upper()) except ConnectionResetError: break conn.close() server.close()
client:
import socket client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) client.connect(('127.0.0.1',8080)) while 1: msg = input('content:').strip() if not msg: continue client.send(msg.encode('utf-8')) data = client.recv(1024) print(data.decode('utf-8')) client.close()