《计算机网络-自顶向下(中文第六版)P112》——基于python3单台主机的TCP(客户机/服务机)实验
作者:互联网
一、客户机
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()
标签:modifiedSentence,socket,sentence,客户机,connectionSocket,第六版,自顶向下,serverPort,client 来源: https://www.cnblogs.com/cnlntr/p/12587119.html