From 6f16649be4ca88d6511d4482be93cd9249846e0d Mon Sep 17 00:00:00 2001 From: xphi Date: Thu, 25 Feb 2021 11:42:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=AC=AC=E4=BA=8C=E5=8D=95?= =?UTF-8?q?=E5=85=83=E5=AE=9E=E9=AA=8C=E7=9A=84=E5=8F=82=E8=80=83=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CODE/chapter2/SMTPClient.py | 66 ------------------- CODE/chapter2/Websever.py | 62 ----------------- .../reference}/ch_socket/SMTPClient.py | 0 .../ch_socket}/UDP_Pinger_Client.py | 0 .../ch_socket}/UDP_Pinger_Server.py | 0 .../reference}/ch_socket/Websever.py | 0 CODE/reference/ch_socket/readme.md | 12 ++++ code/ch_socket/UDP_Pinger_Client.py | 19 ------ code/ch_socket/UDP_Pinger_Server.py | 11 ---- data/appendix/cheat_sheet.tex | 2 +- 10 files changed, 13 insertions(+), 159 deletions(-) delete mode 100644 CODE/chapter2/SMTPClient.py delete mode 100644 CODE/chapter2/Websever.py rename {code => CODE/reference}/ch_socket/SMTPClient.py (100%) rename CODE/{chapter2 => reference/ch_socket}/UDP_Pinger_Client.py (100%) rename CODE/{chapter2 => reference/ch_socket}/UDP_Pinger_Server.py (100%) rename {code => CODE/reference}/ch_socket/Websever.py (100%) create mode 100644 CODE/reference/ch_socket/readme.md delete mode 100644 code/ch_socket/UDP_Pinger_Client.py delete mode 100644 code/ch_socket/UDP_Pinger_Server.py diff --git a/CODE/chapter2/SMTPClient.py b/CODE/chapter2/SMTPClient.py deleted file mode 100644 index dd332d2..0000000 --- a/CODE/chapter2/SMTPClient.py +++ /dev/null @@ -1,66 +0,0 @@ -from socket import * -from base64 import * - -mailfrom = "mail from:2@test.com\r\n" -rcptto = "rcpt to:1@test.com\r\n" -data = "data\r\n" -quitmsg = "quit\r\n" - -msg = "\r\n I love computer networks!" -endmsg = "\r\n.\r\n" -#此处使用易邮邮件服务器软件搭建了一个内网邮件服务器 -mailserver = '10.130.82.62' -mailport = 25 -connectaddress = (mailserver, mailport) -# Create socket called clientSocket and establish a TCP connection with mailserver -clientSocket = socket(AF_INET, SOCK_STREAM) -clientSocket.connect(connectaddress) - -recv = clientSocket.recv(1024) -print (recv) -if recv[:3] != '220': - print ('220 reply not received from server.') -# Send HELO command and print server response. -heloCommand = 'HELO Alice\r\n' -clientSocket.send(bytes(heloCommand.encode())) -recv1 = clientSocket.recv(1024) -print (recv1.decode()) -if recv1[:3] != '250': - print ('250 reply not received from server.') - -#print('000000000000000') -#从命令和打印服务器响应发送邮件。 -login = b'auth login\r\n' -clientSocket.send(login) -recv2 = clientSocket.recv(1024).decode('utf-8') -print ('222+',recv2) - -userCommand = b64encode('2@test.com'.encode('utf-8')) -clientSocket.send((str(userCommand,encoding='utf-8')+'\r\n').encode()) -recv3 = clientSocket.recv(1024).decode('utf-8') -print ('333+',recv3) - -password = b64encode('2'.encode('utf-8')) -clientSocket.send((str(password,encoding='utf-8')+'\r\n').encode()) -recv4 = clientSocket.recv(1024).decode('utf-8') -print ('444+',recv4) - -#print('0000000000000000') - -clientSocket.send(bytes(mailfrom.encode())) -check = clientSocket.recv(1024) -print(check) -clientSocket.send(bytes(rcptto.encode()))#将RCPT发送到命令和打印服务器响应。 -check1 = clientSocket.recv(1024) -print(check1) -clientSocket.send(bytes(data.encode()))#发送数据命令和打印服务器响应。 -check2 = clientSocket.recv(1024) -print(check2) -clientSocket.send(bytes((mailfrom+msg+endmsg).encode()))#发送消息数据。 -check3 = clientSocket.recv(1024) -print(check3) -#发送退出命令并获得服务器响应。 -clientSocket.send(bytes(quitmsg.encode())) -check4 = clientSocket.recv(1024) -print(check4) -clientSocket.close() diff --git a/CODE/chapter2/Websever.py b/CODE/chapter2/Websever.py deleted file mode 100644 index 1d72b0d..0000000 --- a/CODE/chapter2/Websever.py +++ /dev/null @@ -1,62 +0,0 @@ -# Import socket module -from socket import * -import sys # In order to terminate the program - -# Create a TCP server socket -#(AF_INET is used for IPv4 protocols) -#(SOCK_STREAM is used for TCP) - -serverSocket = socket(AF_INET, SOCK_STREAM) - -# Assign a port number -serverPort = 80 - -# Bind the socket to server address and server port -serverSocket.bind(("", serverPort)) - -# Listen to at most 1 connection at a time -serverSocket.listen(1) - -# Server should be up and running and listening to the incoming connections - -while True: - print('The server is ready to receive') - - # Set up a new connection from the client - connectionSocket, addr = serverSocket.accept() - - # If an exception occurs during the execution of try clause - # the rest of the clause is skipped - # If the exception type matches the word after except - # the except clause is executed - try: - # Receives the request message from the client - message = connectionSocket.recv(1024).decode() - # Extract the path of the requested object from the message - # The path is the second part of HTTP header, identified by [1] - filename = message.split()[1] - # Because the extracted path of the HTTP request includes - # a character '\', we read the path from the second character - f = open(filename[1:]) - # Store the entire contenet of the requested file in a temporary buffer - outputdata = f.read() - # Send the HTTP response header line to the connection socket - connectionSocket.send("HTTP/1.1 200 OK\r\n\r\n".encode()) - - # Send the content of the requested file to the connection socket - for i in range(0, len(outputdata)): - connectionSocket.send(outputdata[i].encode()) - connectionSocket.send("\r\n".encode()) - - # Close the client connection socket - connectionSocket.close() - - except IOError: - # Send HTTP response message for file not found - connectionSocket.send("HTTP/1.1 404 Not Found\r\n\r\n".encode()) - connectionSocket.send("

404 Not Found

\r\n".encode()) - # Close the client connection socket - connectionSocket.close() - -serverSocket.close() -sys.exit()#Terminate the program after sending the corresponding data diff --git a/code/ch_socket/SMTPClient.py b/CODE/reference/ch_socket/SMTPClient.py similarity index 100% rename from code/ch_socket/SMTPClient.py rename to CODE/reference/ch_socket/SMTPClient.py diff --git a/CODE/chapter2/UDP_Pinger_Client.py b/CODE/reference/ch_socket/UDP_Pinger_Client.py similarity index 100% rename from CODE/chapter2/UDP_Pinger_Client.py rename to CODE/reference/ch_socket/UDP_Pinger_Client.py diff --git a/CODE/chapter2/UDP_Pinger_Server.py b/CODE/reference/ch_socket/UDP_Pinger_Server.py similarity index 100% rename from CODE/chapter2/UDP_Pinger_Server.py rename to CODE/reference/ch_socket/UDP_Pinger_Server.py diff --git a/code/ch_socket/Websever.py b/CODE/reference/ch_socket/Websever.py similarity index 100% rename from code/ch_socket/Websever.py rename to CODE/reference/ch_socket/Websever.py diff --git a/CODE/reference/ch_socket/readme.md b/CODE/reference/ch_socket/readme.md new file mode 100644 index 0000000..addcd4e --- /dev/null +++ b/CODE/reference/ch_socket/readme.md @@ -0,0 +1,12 @@ +# 套接字编程参考实现 + +# 说明 + +这个目录中的四个Python文件是实验单元“基于套接字的网络编程” +中三个实验的参考实现代码。 + +其中: + +* UDP_Pinger_*.py 是实验“套接字基础与UDP通信”的参考实现 +* Websever.py 是实验“TCP通信与Web服务器”的参考实现 +* SMTPClient.py 是实验“SMTP客户端实现”的参考实现 \ No newline at end of file diff --git a/code/ch_socket/UDP_Pinger_Client.py b/code/ch_socket/UDP_Pinger_Client.py deleted file mode 100644 index db005cf..0000000 --- a/code/ch_socket/UDP_Pinger_Client.py +++ /dev/null @@ -1,19 +0,0 @@ -from socket import * -import time -serverName = 'localhost' -serverPort = 12000 -clientSocket = socket(AF_INET, SOCK_DGRAM) -clientSocket.settimeout(1) - -for i in range(0, 10): - sendTime = time.time() - message = ('Ping %d %s' % (i + 1, sendTime)).encode() - try: - clientSocket.sendto(message, (serverName, serverPort)) - modifiedMessage, serverAddress = clientSocket.recvfrom(2048) - rtt = time.time() - sendTime - print('Sequence %d: Reply from %s RTT = %.3fs' % (i + 1, serverName, rtt)) - except Exception as e: - print('Sequence %d: Request timed out' % (i + 1)) - -clientSocket.close() diff --git a/code/ch_socket/UDP_Pinger_Server.py b/code/ch_socket/UDP_Pinger_Server.py deleted file mode 100644 index 25bda49..0000000 --- a/code/ch_socket/UDP_Pinger_Server.py +++ /dev/null @@ -1,11 +0,0 @@ -import random -from socket import * -serverSocket = socket(AF_INET, SOCK_DGRAM) -serverSocket.bind(('', 12000)) -while True: - rand = random.randint(0, 10) - message, address = serverSocket.recvfrom(1024) - message = message.upper() - if (rand < 4): - continue - serverSocket.sendto(message, address) diff --git a/data/appendix/cheat_sheet.tex b/data/appendix/cheat_sheet.tex index e96b8bb..0462b18 100644 --- a/data/appendix/cheat_sheet.tex +++ b/data/appendix/cheat_sheet.tex @@ -9,7 +9,7 @@ \begin{itemize} \item \textbf{基本信息:} - 表\ref{tab:a:csheet:basic}汇总了本指导书所收录的实验的实验目的、 + 表\ref{tab:a:csheet:basic}汇总了本指导书所收录实验的实验目的、 难度与建议的课时安排等基本信息; \item \textbf{实验任务与要求:}