server
import socket phone = socket.socket (socket.AF_INET, socket.SOCK_STREAM) phone.bind(('127.0.0.1',8080)) phone.listen(5) conn,client_addr=phone.accept() print(client_addr) #通信循环 while True: data=conn.recv(1024) print("Client data:",data) conn.send(data.upper()) conn.close()
client
import socket client = socket.socket (socket.AF_INET, socket.SOCK_STREAM) client.connect(( ' 127.0.0.1 ' ,8080 )) #communication loop while True: msg=input('>> ').strip() client.send(msg.encode('utf-8')) data =client.recv(1024 ) print (data) #Close the connection client.close()