一、客户机
1 from socket import * 2 serverName = '192.168.137.1' 3 serverPort = 12000 4 clientSocket = socket(AF_INET, SOCK_STREAM) 5 clientSocket.connect((serverName,serverPort)) 6 sentence = input('Input lowercase sentence:') 7 sentence = sentence.encode() 8 clientSocket.send(sentence) 9 modifiedSentence = clientSocket.recv(1024) 10 modifiedSentence = modifiedSentence.decode() 11 print('From Server:' , modifiedSentence) 12 clientSocket.close()
二、服务机
1 from socket import * 2 serverPort = 12000 3 serverSocket = socket(AF_INET,SOCK_STREAM) 4 serverSocket.bind(("", serverPort)) 5 serverSocket.listen(1) 6 print('The server is ready to receive') 7 while True: 8 connectionSocket,addr = serverSocket.accept() 9 sentence = connectionSocket.recv(1024) 10 capitalizedSentence = sentence.upper() 11 connectionSocket.send(capitalizedSentence) 12 connectionSocket.close()