"Computer Network - top-down (Chinese sixth edition) P112" - based python3 single host TCP (client / server) experiment

A client

 1 from socket import *
 2 serverName = '192.167.117.100'
 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()

Second, the service machine

 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()

 

Guess you like

Origin www.cnblogs.com/cnlntr/p/12587119.html