服务端 from socket import * server = socket(AF_INET,SOCK_STREAM) server.bind((‘127.0.0.1‘,8080)) server.listen(3) print(‘server startting...‘) while True: conn,addr = server.accept() print(addr) while True: try: data = conn.recv(1024) if not data:break conn.send(data.upper()) except Exception: break conn.close() server.close() 客户端 from socket import * client = socket(AF_INET,SOCK_STREAM) client.connect((‘127.0.0.1‘,8080)) while True: msg = input(‘p;ease:‘).strip() if not msg:continue client.send(msg.encode(‘utf-8‘)) data = client.recv(1024) print(data.decode(‘utf-8‘))
时间: 2024-10-01 07:23:09