《计算机网络-自顶向下(中文第六版)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()

 

上一篇:程序员面试金典 - 面试题 17.13. 恢复空格(DP+Trie树)


下一篇:python实现输入一段英文单词后,倒叙输出