From 9f071eb5a022911e98b893751839e51353c86429 Mon Sep 17 00:00:00 2001 From: freedomlove <335946148@qq.com> Date: Sun, 21 Feb 2021 20:06:18 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=8D=95=E5=85=83=E4=BA=8C+code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CODE/chapter2/SMTPClient.py | 66 ++++++++++++++++++++++++++++++ CODE/chapter2/UDP_Pinger_Client.py | 19 +++++++++ CODE/chapter2/UDP_Pinger_Server.py | 11 +++++ CODE/chapter2/Websever.py | 62 ++++++++++++++++++++++++++++ data/chapter2/section1.tex | 7 ++++ data/chapter2/section2.tex | 6 +++ data/chapter2/section3.tex | 6 +++ data/preface.tex | 2 +- 8 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 CODE/chapter2/SMTPClient.py create mode 100644 CODE/chapter2/UDP_Pinger_Client.py create mode 100644 CODE/chapter2/UDP_Pinger_Server.py create mode 100644 CODE/chapter2/Websever.py diff --git a/CODE/chapter2/SMTPClient.py b/CODE/chapter2/SMTPClient.py new file mode 100644 index 0000000..dd332d2 --- /dev/null +++ b/CODE/chapter2/SMTPClient.py @@ -0,0 +1,66 @@ +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/UDP_Pinger_Client.py b/CODE/chapter2/UDP_Pinger_Client.py new file mode 100644 index 0000000..db005cf --- /dev/null +++ b/CODE/chapter2/UDP_Pinger_Client.py @@ -0,0 +1,19 @@ +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/chapter2/UDP_Pinger_Server.py b/CODE/chapter2/UDP_Pinger_Server.py new file mode 100644 index 0000000..25bda49 --- /dev/null +++ b/CODE/chapter2/UDP_Pinger_Server.py @@ -0,0 +1,11 @@ +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/CODE/chapter2/Websever.py b/CODE/chapter2/Websever.py new file mode 100644 index 0000000..1d72b0d --- /dev/null +++ b/CODE/chapter2/Websever.py @@ -0,0 +1,62 @@ +# 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/data/chapter2/section1.tex b/data/chapter2/section1.tex index a2cc234..5022c15 100644 --- a/data/chapter2/section1.tex +++ b/data/chapter2/section1.tex @@ -60,6 +60,13 @@ UDP报文没有可靠性保证、顺序保证和流量控制字段等,可靠 \item 服务器程序(实验步骤中已给出)。 \end{itemize} + +参考资料: + +\begin{itemize} + \item {J.F Kurose and K.W. Ross, 计算机网络自顶向下方法(第7版)} +\end{itemize} + \subsection{实验步骤} \label{subsec:c2_s1_procedure} diff --git a/data/chapter2/section2.tex b/data/chapter2/section2.tex index 6c7b08f..59801ec 100644 --- a/data/chapter2/section2.tex +++ b/data/chapter2/section2.tex @@ -47,6 +47,12 @@ Web服务器接受并解析HTTP请求,从服务器的文件系统中获取请 \item 部分代码(实验步骤中已给出)。 \end{itemize} +参考资料: + +\begin{itemize} + \item {J.F Kurose and K.W. Ross, 计算机网络自顶向下方法(第7版)} +\end{itemize} + \subsection{实验步骤} \label{subsec:c2_s2_procedure} diff --git a/data/chapter2/section3.tex b/data/chapter2/section3.tex index ecb3db5..5b53105 100644 --- a/data/chapter2/section3.tex +++ b/data/chapter2/section3.tex @@ -76,6 +76,12 @@ RFC5321给出了SMTP的定义。SMTP有众多出色的性质, \item 部分代码(实验步骤中已给出)。 \end{itemize} +参考资料: + +\begin{itemize} + \item {J.F Kurose and K.W. Ross, 计算机网络自顶向下方法(第7版)} +\end{itemize} + \subsection{实验步骤} \label{subsec:c2_s3_procedure} diff --git a/data/preface.tex b/data/preface.tex index 3a80219..f0942cc 100644 --- a/data/preface.tex +++ b/data/preface.tex @@ -22,7 +22,7 @@ \begin{itemize} \item 第一单元:谢怡、陈建发、洪劼超、雷蕴奇,厦门大学 - \item 第二单元:吴荻,国防科技大学 + \item 第二单元:吴荻、徐明、夏竟、胡罡,国防科技大学 \item 第三单元:张晓丽,昆明理工大学 \end{itemize} From eae31c642cfe39961433a376fd3a5e8c887782fa Mon Sep 17 00:00:00 2001 From: Xphi Date: Wed, 24 Feb 2021 17:27:00 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E7=AB=A0=E5=8F=82=E8=80=83=E5=AE=9E=E7=8E=B0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/ch_socket/SMTPClient.py | 66 +++++++++++++++++++++++++++++ code/ch_socket/UDP_Pinger_Client.py | 19 +++++++++ code/ch_socket/UDP_Pinger_Server.py | 11 +++++ code/ch_socket/Websever.py | 62 +++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 code/ch_socket/SMTPClient.py create mode 100644 code/ch_socket/UDP_Pinger_Client.py create mode 100644 code/ch_socket/UDP_Pinger_Server.py create mode 100644 code/ch_socket/Websever.py diff --git a/code/ch_socket/SMTPClient.py b/code/ch_socket/SMTPClient.py new file mode 100644 index 0000000..bfec76a --- /dev/null +++ b/code/ch_socket/SMTPClient.py @@ -0,0 +1,66 @@ +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/ch_socket/UDP_Pinger_Client.py b/code/ch_socket/UDP_Pinger_Client.py new file mode 100644 index 0000000..db005cf --- /dev/null +++ b/code/ch_socket/UDP_Pinger_Client.py @@ -0,0 +1,19 @@ +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 new file mode 100644 index 0000000..25bda49 --- /dev/null +++ b/code/ch_socket/UDP_Pinger_Server.py @@ -0,0 +1,11 @@ +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/code/ch_socket/Websever.py b/code/ch_socket/Websever.py new file mode 100644 index 0000000..d901408 --- /dev/null +++ b/code/ch_socket/Websever.py @@ -0,0 +1,62 @@ +# 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 From e6d12247b961bd290062dd7eb53eea3839911d50 Mon Sep 17 00:00:00 2001 From: xphi Date: Thu, 25 Feb 2021 01:29:28 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=99=84=E5=BD=95?= =?UTF-8?q?=EF=BC=9A=E9=80=9F=E6=9F=A5=E6=89=8B=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- book/instructions.cls | 1 + book/instructions.tex | 7 + data/appendix/cheat_sheet.tex | 252 ++++++++++++++++++++++++++++++++++ data/appendix/ensp.tex | 34 ++--- 4 files changed, 277 insertions(+), 17 deletions(-) create mode 100644 data/appendix/cheat_sheet.tex diff --git a/book/instructions.cls b/book/instructions.cls index ce7454d..b186e68 100644 --- a/book/instructions.cls +++ b/book/instructions.cls @@ -66,6 +66,7 @@ \newcommand{\sectionbreak}{\clearpage} \usepackage{setspace} +\usepackage{lscape} % \usepackage{makecell} %====================================================================== diff --git a/book/instructions.tex b/book/instructions.tex index cf4180a..2dcd0bb 100644 --- a/book/instructions.tex +++ b/book/instructions.tex @@ -101,12 +101,19 @@ \renewcommand{\thetable}{\Alph{chapter}--\arabic{table}} \renewcommand{\chaptername}{附录\Alph{chapter}} +% 速查手册 +\graphicspath{{../figure/appendixes/cheat_sheet/}} +\input{../data/appendix/cheat_sheet} + +% 华为网络仿真平台eNSP简介 \graphicspath{{../figure/appendixes/ensp/}} \input{../data/appendix/ensp} +% 可编程网络平台-操作手册 \graphicspath{{../figure/appendixes/openbox/}} \input{../data/appendix/openbox} +% FAST软件编程入门指南 \graphicspath{{../figure/appendixes/fast/}} \input{../data/appendix/fast} diff --git a/data/appendix/cheat_sheet.tex b/data/appendix/cheat_sheet.tex new file mode 100644 index 0000000..e96b8bb --- /dev/null +++ b/data/appendix/cheat_sheet.tex @@ -0,0 +1,252 @@ +%# -*- coding: utf-8-unix -*- + +\chapter{速查手册} +\label{app:cheatsheet} + +为了方便老师根据本学校的实际教学情况选择实验内容, +特将本指导书所收录全部实验的基本信息汇总于此, +供老师快速查阅。 + +\begin{itemize} + \item \textbf{基本信息:} + 表\ref{tab:a:csheet:basic}汇总了本指导书所收录的实验的实验目的、 + 难度与建议的课时安排等基本信息; + + \item \textbf{实验任务与要求:} + 表\ref{tab:a:csheet:task-summary}汇总了本指导书所收录的实验的实验任务与要求; + + \item \textbf{考核方式:} + 建议根据每次实验上交实验报告,上课实际操作、作品评价等内容综合进行形成性考核, + 通用的实验成绩形成性考核标准见表\ref{tab:a:csheet:criterion}, + 各实验如有特殊要求,还在正文中指出了各自的细节评分标准。 +\end{itemize} + +\renewcommand{\arraystretch}{1.5} + +\begin{table}[!ht] + \small + \centering + \caption{实验成绩形成性考核标准} + \label{tab:a:csheet:criterion} + \begin{tabular}{|m{2.5cm}<{\centering}|m{11cm}|} \hline + \heiti 考核项目 & \multicolumn{1}{c|}{\heiti 评分标准} \\ \hline + \multirow{5}{2.5cm}{\centering 实验速度\\ (20分)} + & 完成时间排名<= 20\%获得20分;\\ \cline{2-2} + & 20\%<完成时间排名<= 40\%获得18分;\\ \cline{2-2} + & 40\%<完成时间排名<= 60\%获得16分;\\ \cline{2-2} + & 60\%<完成时间排名<= 80\%获得14分;\\ \cline{2-2} + & 完成时间排名>80\%获得12分。 \\ \hline + \multirow{4}{2.5cm}{\centering 实验完成情况\\(30分)} + & 熟练掌握实验所需的设备、器件和软件(5分)\\ \cline{2-2} + & 掌握正确的实验方法(5分)\\ \cline{2-2} + & 独立完成实验(10分)\\ \cline{2-2} + & 根据实验结果提出实验改进措施并持续改进实验。(10分)\\ \hline + \multirow{4}{2.5cm}{\centering 实验报告\\(50分)} + & 格式正确、规范。(10分)\\ \cline{2-2} + & 完整记录实验过程。(10分)\\ \cline{2-2} + & 在实验过程中运用所学的的理论知识发现问题并解决问题。(20分)\\ \cline{2-2} + & 能够正确记录和分析实验结果。(10分)\\ \hline + \end{tabular} +\end{table} + +\newcommand*{\tabitem}[1]{\hangindent=1em\makebox[1em]{$\bullet$}#1} +\small +\begin{longtable}{|m{1.5cm}<{\centering} + |m{1.7cm}<{\centering} + |m{8.5cm} + |m{0.7cm}<{\centering} + |m{1.2cm}|} + \caption{\label{tab:a:csheet:basic}实验基本信息}\\ + + \hline + \heiti 单元 & \multicolumn{1}{c|}{\heiti 实验} & + \multicolumn{1}{c|}{\heiti 目的} & + \multicolumn{1}{c|}{\heiti 课时} & + \multicolumn{1}{c|}{\heiti 难度} \\ \hline + \endfirsthead + \hline + \heiti 单元 & \multicolumn{1}{c|}{\heiti 实验} & + \multicolumn{1}{c|}{\heiti 目的} & + \multicolumn{1}{c|}{\heiti 课时} & + \multicolumn{1}{c|}{\heiti 难度} \\ \hline + \endhead + + \multirow{3}{1.5cm}{\centering 网络抓包与协议分析} + & Wireshark软件使用和ARP分析 & + \tabitem{掌握Wireshark的基本操作,使用捕获过滤器和显示过滤器,抓取和分析有线局域网的数据包。} \par + \tabitem{掌握以太网MAC帧的基本结构。} \par + \tabitem{掌握ARP协议的特点及工作过程。} + & 2 & $\star$ \\ \cline{2-5} + + & IP和ICMP分析 & + \tabitem{熟练使用Wireshark软件,观察IP数据报的基本结构,分析数据报的分片过程;} \par + \tabitem{掌握基于ICMP协议的ping和traceroute命令及其工作过程。} + & 2 & $\star\star$ \\ \cline{2-5} + + & TCP与拥塞控制 & + \tabitem{运用Wireshark观察分析TCP协议报文,分析通信时序,理解TCP的工作过程;} \par + \tabitem{分析TCP连接管理、流量控制和拥塞控制的过程,能够分析TCP的性能问题。} + & 4 & $\star\star\star$ \\ \hline + + & 套接字基础与UDP通信 & + \tabitem{掌握Python中UDP套接字编程基础;} \par + \tabitem{掌握使用UDP套接字发送和接收数据包,以及设置正确的套接字超时;} \par + \tabitem{了解Ping应用程序及其在计算数据包丢失率等统计数据方面的有用性。} + & 2 & $\star$ \\ \cline{2-5} + + 基于套接字的网络程序设计 + & TCP通信与Web服务器 & + \tabitem{掌握Python中TCP套接字编程基础;} \par + \tabitem{理解HTTP报文格式;} \par + \tabitem{了解开发简单Web服务器的流程。} + & 2 & $\star\star$ \\ \cline{2-5} + + & SMTP客户端实现 & + \tabitem{理解和掌握Python中TCP套接字编程基础;} \par + \tabitem{理解SMTP报文格式;} \par + \tabitem{了解开发简单应用程序的流程。} + & 4 & $\star\star\star$ \\ \hline + + 组网基础 + & 静态路由 & + \tabitem{掌握静态路由协议;} \par + \tabitem{理解路由器工作原理;} \par + \tabitem{掌握路由器相关的配置、检测操作。} + & 2 & $\star$ \\ \hline + + \multirow{2}{1.5cm}{\centering 组网基础} + & 动态路由(RIP) & + \tabitem{理解动态路由协议RIP的工作原理;} \par + \tabitem{掌握采用动态路由协议RIP进行网络设计的基本原则和方法。} + & 2 & $\star\star$ \\ \cline{2-5} + + & 动态路由(OSPF) & + \tabitem{理解动态路由协议OSPF的工作原理;} \par + \tabitem{掌握采用动态路由协议OSPF进行网络设计的基本原则和方法。} + & 2 & $\star\star\star$ \\ \hline + + \multirow{3}{1.5cm}{\centering 路由器实现} + & 二层交换机实现& + \tabitem{掌握二层以太网数据收发方法;} \par + \tabitem{熟悉二层交换机的交换原理、分组处理流程和转发表的老化功能与处理方法。} + & 3 & $\star\star$ \\ \cline{2-5} + + & 三层路由器实现 & + \tabitem{掌握二层以太网帧的数据结构、ARP协议和IP协议等,熟悉各协议的解析与封装过程;} \par + \tabitem{掌握路由器数据平面与控制平面的切分及工作处理流程的不同。} + & 2 & $\star\star\star$ \\ \cline{2-5} + + & 三层路由器组网 & + \tabitem{掌握路由器控制平台的工作任务及工作原理,熟悉多种路由协议的工作原理及适应范围。} + & 2 & $\star\star$ \\ \hline + + \multirow{3}{1.5cm}{\centering 软件定义网络与网络测量} + & 基于OpenFlow的SDN交换机 & + \tabitem{了解SDN的概念及三层结构原理,掌握Open Flow协议格式、内容、工作流程及对应功能作用;} \par + \tabitem{掌握SDN控制器北向API使用方法,可通过API编程获取和设置交换机数据。} + & 3 & $\star\star\star\star$ \\ \cline{2-5} + + & 基于OpenFlow的拓扑测量 & + \tabitem{了解OpenFlow协议中PACKET\_IN和链路探测协议(LLDP)的工作原理和拓扑构建方法。} + & 3 & $\star\star\star\star$ \\ \cline{2-5} + + & 纳秒级高精度硬件测量 & + \tabitem{掌握网络测试的常用方法,了解在测试过程中影响测量精度的原因及如何提高测量精度。} + & 3 & $\star\star\star\star$ \\ \hline +\end{longtable} + +\begin{landscape} + +\renewcommand{\arraystretch}{1.5} +\small +\begin{longtable}{|m{1.5cm}<{\centering} + |m{2cm}<{\centering} + |m{8cm}|m{9cm}|} + \caption{\label{tab:a:csheet:task-summary}实验任务与要求汇总}\\ + + \hline + \heiti 单元 & \heiti 实验 & + \multicolumn{1}{c|}{\heiti 任务} & + \multicolumn{1}{c|}{\heiti 要求} \\ \hline + \endfirsthead + \hline + \heiti 单元 & \heiti 实验 & + \multicolumn{1}{c|}{\heiti 任务} & + \multicolumn{1}{c|}{\heiti 要求} \\ \hline + \endhead + \hline + \endfoot + \endlastfoot + + \multirow{3}{1.5cm}{\centering 网络抓包与协议分析} & + Wireshark软件使用和ARP分析 & + 熟悉使用Wireshark主菜单、工具栏和主窗口;掌握捕获过滤器和显示过滤器;捕获有线局域网的数据包并保存;了解MAC地址组成;观察以太网帧的首部和尾部,计算帧长度和FCS;抓取ARP请求和应答报文,分析其工作过程。 & + 详见实验指导书。安装并学习使用Wireshark软件,现场检查操作正确性;按步骤进行以太网帧和ARP命令的实验;收集分析数据,回答思考题,并编写实验报告。\\ \cline{2-4} + + & IP和ICMP分析 & + 执行ping命令,观察IP数据报和ICMP询问报文的结构;改变ping命令的参数,观察IP数据报分片;执行Traceroute命令,观察ICMP差错报文的结构,并分析其工作原理。 & + 详见实验指导书,根据老师的安排确定实验内容;部署实验环境;按步骤进行实验;收集分析数据,回答思考题,并编写实验报告。\\ \cline{2-4} + + & TCP与拥塞控制 & + 观察正常TCP连接中三次握手与四次挥手报文,绘制出时序图,并标出双方TCP状态的变化;观察分析TCP通信过程中各类异常(连接建立过程的异常、SNY洪泛攻击、数据超时和乱序等);运行基于TCP传输的Python程序,观察收发报文中通告窗口的变化,了解滑动窗口工作的原理;观察大文件传输过程,分析识别TCP的慢启动、拥塞避免、快速恢复等拥塞控制过程;运行iperf3进行网络性能测试,比较不同拥塞控制性能表现。 & + 详见实验指导书,根据老师的安排确定实验内容;部署实验环境;按步骤进行实验;收集分析数据,回答思考题,并编写实验报告。\\ \hline + + \multirow{3}{1.5cm}{基于套接字的网络程序设计} & + + 套接字基础与UDP通信& + 使用Python编写一个简单的,非标准的基于UDP的ping程序,该程序能够发送简单的ping报文,接收从服务器往返的对应pong报文,并确定从该客户发送ping报文到接收pong报文为止的时延(往返时延RTT)。& + 根据实验指导书,参考服务器端代码,完成客户端代码,测试和分析实验结果。\\ \cline{2-4} + + & TCP通信与Web服务器 & + 使用Python开发一个一次能处理一个HTTP请求的Web服务器。& + 根据实验指导书,在提供的框架基础上完成客户端代码,测试和分析实验结果。\\ \cline{2-4} + + & SMTP客户端实现 & + 使用Python开发一个能够发送文本信息的简单的SMTP客户端。& + 根据实验指导书,在提供的框架基础上完成客户端代码,测试和分析实验结果。\\ \hline + + \multirow{3}{1.5cm}{组网基础} & + + 静态路由& + 完成路由规划并在华为网络模拟器eNSP中完成IP地址和静态路由的配置,掌握采用静态路由进行网络设计的基本原则和方法,理解静态路由的工作原理。& + 根据实验指导书,完成实验环境的安装和配置,按步骤进行实验,观察和记录实验结果并编写实验报告。\\ \cline{2-4} + + & 动态路由(RIP) & + 完成路由规划并在华为网络模拟器eNSP中完成IP地址和RIP协议的配置,掌握采用动态路由协议RIP进行网络设计的基本原则和方法,理解动态路由协议RIP的工作原理。& + 根据实验指导书,完成实验环境的安装和配置,按步骤进行实验,观察和记录实验结果并编写实验报告。\\ \cline{2-4} + + & 动态路由(OSPF) & + 完成路由规划并在华为网络模拟器eNSP中完成IP地址和OSPF协议的配置,掌握采用动态路由协议OSPF进行网络设计的基本原则和方法,理解动态路由协议OSPF的工作原理。& + 根据实验指导书,完成实验环境的安装和配置,按步骤进行实验,观察和记录实验结果并编写实验报告。\\ \hline + + \multirow{3}{1.5cm}{路由器实现} & + + 二层交换机实现& + 基于网络创新实验开发平台,使用C语言编程开发实现一个软件二层交换机系统原型;实现二层以太网分组交换功能;完成二层分组数据解析、MAC表学习与查找以及MAC表老化功能。& + 基于网络创新实验开发平台的参考示例程序完成;实现测试主机在多个设备端口间均能ping通;拔掉交换机端口网线1分钟后触发该端口MAC表项老化和删除功能;主机网线连接端口变化时,打印输出原来连接端口与新连接端口号信息。\\ \cline{2-4} + + & 三层路由器实现 & + 基于网络创新实验开发平台,使用C语言编程开发实现一个软件三层交换机系统原型,实现不同网段的分组通信功能;完成端口ARP协议交互功能;完成FIB表获取与查询功能;完成分组数据二层与三层的协议解析与封装功能。& + 基于网络创新实验开发平台的参考示例程序完成;分组数据仅需支持ARP与IPv4协议格式;不同端口内不同网段的主机均能相互ping通;FIB表内容从系统内核获取并能周期刷新;根据协议解析内容区分打印数据平面分组与控制平面分组数据流摘要信息,两种类型数据分开处理;区分打印ARP主动请求学习与被动响应的处理过程;在测试主机ping 8.8.8.8,观察路由器FIB查表的执行步骤,分析原因。\\ \cline{2-4} + + & 三层路由组网实现& + 在自研三层路由器原型系统上运行Quagga协议,并正确配置路由协议,使用两个以上路由器原型系统实现多网段互联互通。& + 正确安装Quagga运行环境,正确配置RIP与OSPF两种路由协议;单独运行一种路由协议能正确学习到远端网络路由转发表内容;各网段在不同路由协议使用情况下均能相互ping通;与不同的实验组同学组网验证;断开某个网段的连线,验证路由器FIB表的更新(原来存在的表项消失)。\\ \hline + + \multirow{3}{1.5cm}{软件定义网络与网络测量} & + + 基于OpenFlow的SDN交换机& + 基于网络创新实验开发平台,使用C语言编程开发实现一个SDN交换机系统原型,实现SDN交换机的基本通信及配置管理功能;& + 参考OpenFlow~1.3协议格式,实现指定的消息;能利用PACKET\_IN报文上报端口输入的完整二层以太网数据;ACTION动作可只支持端口转发与丢弃;基于实验平台的硬件流表管理API接口将流表配置的OFPT\_FLOW\_MOD消息内容同步到硬件表项中。\\ \cline{2-4} + + & 基于OpenFlow的拓扑测量 & + 使用多个自研的SDN交换机组网互连,通过LLDP的链路探测协议完成全网拓扑的构建;编程展示网络拓扑信息图。& + 基于自研的SDN交换机原型实现;最少使用3台设备;SDN控制器建议使用Floodlight;区分打印交换机自身发出的LLDP协议分组和接收到其他交换机的LLDP分组,显示分组摘要信息;通过REST API接口获取SDN控制器的链路连接信息与设备信息;基于WEB网页编程展示网络拓扑图。变换不同网络连接结构,刷新验证WEB页面的拓扑图,要求与真实网络连接关系一致。3台设备中间使用一台普通交换机连接,再次验证拓扑图显示,分析原因。\\ \cline{2-4} + + & 纳秒级高精度硬件测量& + 基于网络创新实验开发平台,完成测量节点到服务器的RTT延时测量、网络带宽测量以及不同长度网线传输延时测量。& + 基于网络创新实验开发平台的测量工具与源码;测量服务器RTT延时的报文使用ping协议格式;测量网络带宽时,在网络内使用固定背景流量大于500M,测量分组的大小设置64与1500,计算测量带宽差异;测量1米与10米网线的传输延时,判断并分析二者的延时差别。\\ \hline + +\end{longtable} + +\end{landscape} \ No newline at end of file diff --git a/data/appendix/ensp.tex b/data/appendix/ensp.tex index 35eb905..1e67cba 100644 --- a/data/appendix/ensp.tex +++ b/data/appendix/ensp.tex @@ -1,17 +1,17 @@ %# -*- coding: utf-8-unix -*- -\chapter{华为网络仿真平台ENSP简介} -\label{app:ENSP} +\chapter{华为网络仿真平台eNSP简介} +\label{app:eNSP} -掌握华为ENSP软件的安装、操作和使用;熟悉华为模拟设备的基本配置及其配置命令; +掌握华为eNSP软件的安装、操作和使用;熟悉华为模拟设备的基本配置及其配置命令; 掌握常见问题的解决方法。 -\subsection{本地安装华为ENSP软件} -可在本机安装华为ENSP软件。 +\subsection{本地安装华为eNSP软件} +可在本机安装华为eNSP软件。 -安装版本如选择的是华为ENSP17年版本的, +安装版本如选择的是华为eNSP17年版本的, 则安装包里自带所需软件,则无需另行下载, -默认4款软件全部安装;如选择的是华为ENSP最新版即19年的版本, +默认4款软件全部安装;如选择的是华为eNSP最新版即19年的版本, 在安装之前须自行下载安装以下3款软件且最好版本号一致。 eNSP的正常实验依赖与WinPcap、Wireshark和VirtualBox三款软件, @@ -21,7 +21,7 @@ eNSP的正常实验依赖与WinPcap、Wireshark和VirtualBox三款软件, \begin{table}[!ht] \small \centering - \caption{华为ENSP需安装软件及对应版本号} + \caption{华为eNSP需安装软件及对应版本号} \label{tab:a:wireshark_eth-format} \begin{tabular}{|c|c|} \hline \heiti 软件类别 & \heiti 版本号\\ \hline @@ -31,24 +31,24 @@ eNSP的正常实验依赖与WinPcap、Wireshark和VirtualBox三款软件, \end{tabular} \end{table} -\textbf{安装注意事项:}先装3款基础软件后再安装华为ENSP, +\textbf{安装注意事项:}先装3款基础软件后再安装华为eNSP, 所有安装都建议默认安装,最好不要更改安装盘,更不要设置中文安装目录, 否则可能使用时会出现问题,则只能重装。 -\subsection{华为ENSP操作和使用} -华为ENSP的操作和使用,强烈建议查看其帮助,并不断实践,如图: +\subsection{华为eNSP操作和使用} +华为eNSP的操作和使用,强烈建议查看其帮助,并不断实践,如图: \begin{figure}[!ht] \centering - \includegraphics[width=13cm]{ENSP-UI} - \caption{华为ENSP界面} - \label{fig:a1_ENSP-ui} + \includegraphics[width=13cm]{eNSP-UI} + \caption{华为eNSP界面} + \label{fig:a1_eNSP-ui} \end{figure} \begin{figure}[!ht] \centering - \includegraphics[width=13cm]{ENSP-help} - \caption{华为ENSP帮助界面} - \label{fig:a1_ENSP-help} + \includegraphics[width=13cm]{eNSP-help} + \caption{华为eNSP帮助界面} + \label{fig:a1_eNSP-help} \end{figure} \subsection{华为模拟设备的基本配置及其配置命令} From c0d22479e7a2244f09b1393b293af12fce020838 Mon Sep 17 00:00:00 2001 From: xphi Date: Thu, 25 Feb 2021 10:59:15 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E4=B8=BAv0.1a=EF=BC=8C=E4=BF=AE=E6=94=B9=E5=89=8D=E8=A8=80?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=AD=A3=E9=83=A8=E5=88=86=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 6 ++--- book/instructions.tex | 5 ++-- data/appendix/fast.tex | 4 +-- data/appendix/openbox.tex | 8 +++--- data/ch_ensp/preface.tex | 2 +- data/ch_wireshark/sec_arp.tex | 35 +++++++++++++------------- data/cover.tex | 4 +-- data/preface.tex | 46 ++++++++++++++++++++--------------- 8 files changed, 60 insertions(+), 50 deletions(-) diff --git a/Readme.md b/Readme.md index 97cb99f..0a65d4e 100644 --- a/Readme.md +++ b/Readme.md @@ -1,11 +1,11 @@ -# 计算机网络 共同实验指导书 +# 计算机网络 实验指导书 ## 1. 概述 -计算机网络共同实验指导书是由计算机网络教案社区发起, +计算机网络实验指导书是由计算机网络教案社区发起, 配合社区网络教案,由各高校专家教授共同联合编纂的一份实验指导材料。 -本项目工程包含了“计算机网络共同实验指导书”使用的各类相关文件。 +本项目工程包含了“计算机网络实验指导书”使用的各类相关文件。 ## 2. 目录结构 diff --git a/book/instructions.tex b/book/instructions.tex index 2dcd0bb..bcd3883 100644 --- a/book/instructions.tex +++ b/book/instructions.tex @@ -14,8 +14,9 @@ % 版本声明 \clearpage \begin{center} - \Large{\sffamily\bfseries\heiti Version 0.4} \\ \vspace{2em} - \Large{\sffamily\bfseries\heiti 发布日期: \today} \\ \vspace{1em} + \Large{Version 1.0$\alpha$} \\ \vspace{2em} + \Large{发布日期: {\number\year 年 \number\month 月 \number\day 日}} + % \\ \vspace{1em} \end{center} % \vfill diff --git a/data/appendix/fast.tex b/data/appendix/fast.tex index 954ca99..f116ec9 100644 --- a/data/appendix/fast.tex +++ b/data/appendix/fast.tex @@ -42,8 +42,8 @@ FAST的软硬件代码和相关工具下载请查看文章结尾的百度网盘 \begin{table}[!ht] \small \centering - \caption{以太网帧格式} - \label{tab:c:wireshark_eth-format} + \caption{FAST代码目录结构} + \label{tab:a:fast_dir-structure} \begin{tabular}{|c|c|c|} \hline \heiti 序号 & \heiti 目录名称 & \heiti 说明\\ \hline 1 & include & 开发所用头文件目录\\ \hline diff --git a/data/appendix/openbox.tex b/data/appendix/openbox.tex index af18bc2..e5ce51d 100644 --- a/data/appendix/openbox.tex +++ b/data/appendix/openbox.tex @@ -86,8 +86,8 @@ OpenBox-S4是一款软硬件全可编程网络实验平台, \begin{table}[!htp] \small \centering - \caption{以太网帧格式} - \label{tab:c:wireshark_eth-format} + \caption{所需实验物品} + \label{tab:a:openbox_items} \begin{tabular}{|c|c|} \hline \heiti 测试项 & \heiti 所需物品 \\ \hline 上电检测 & 一台设备及电源\\ \hline @@ -334,7 +334,7 @@ OpenBox-S4是一款软硬件全可编程网络实验平台, \centering \includegraphics[width=11cm]{f25} \caption{过滤指定ip地址报文} - \label{fig:a:ob_f24} + \label{fig:a:ob_f25} \end{figure} \item 如过滤mac地址为“01:00:5e:7f:ff:fa”的报文, 则输入过滤条件 “eth.addr == 01:00:5e:7f:ff:fa”如下所示。 @@ -343,7 +343,7 @@ OpenBox-S4是一款软硬件全可编程网络实验平台, \centering \includegraphics[width=11cm]{f26} \caption{过滤指定mac地址报文} - \label{fig:a:ob_f24} + \label{fig:a:ob_f26} \end{figure} \item 由于篇幅的原因,wireshark的其他使用,在本文档就不再说明了。 \end{enumerate} diff --git a/data/ch_ensp/preface.tex b/data/ch_ensp/preface.tex index 1ae90af..3678d82 100644 --- a/data/ch_ensp/preface.tex +++ b/data/ch_ensp/preface.tex @@ -6,6 +6,6 @@ 掌握静态路由、动态路由RIP和OSPF协议的配置,并理解分析静态与动态路由的区别, 两种动态路由协议各自的适用环境,理解网络收敛的概念。 推荐使用华为ENSP模拟器, -在附录\ref{app:ENSP}:《华为网络仿真平台 ENSP 简介》中有该软件的安装、操作和使用, +在附录\ref{app:eNSP}:《华为网络仿真平台 ENSP 简介》中有该软件的安装、操作和使用, 以熟悉华为模拟设备的基本配置及其配置命令, 掌握软件安装常见问题的解决方法。 diff --git a/data/ch_wireshark/sec_arp.tex b/data/ch_wireshark/sec_arp.tex index fafc29e..e20fc4e 100644 --- a/data/ch_wireshark/sec_arp.tex +++ b/data/ch_wireshark/sec_arp.tex @@ -61,7 +61,7 @@ Wireshark可以在Windows、Linux和MacOS操作系统中运行, \caption{以太网帧格式} \label{tab:c:wireshark_eth-format} \begin{tabular}{|c|c|c|c|c|c|} \hline - \heiti 前导字符 & \heiti 目的MAC地址 & \heiti 源MAC地址 & + \heiti 前导字符 & \heiti 目的MAC地址 & \heiti 源MAC地址 & \heiti 类型 & \heiti IP数据报 & \heiti 帧校验\\ \hline 8字节 & 6字节 & 6字节 & 2字节 & 46-1500字节 & 4字节 \\ \hline \end{tabular} @@ -244,29 +244,30 @@ IP地址长度为4字节。每个字段的含义如下: 计算以太网帧的尾部校验和,并与Wireshark显示数值进行比较验证。 \subsubsection{ARP协议分析} \begin{enumerate} - \item 使用\texttt{arp –d}命令(其语法见图\ref{fig:arp-cmd}),清空本机已有的ARP缓存, + \item 使用\texttt{arp –d}命令(其语法见图\ref{fig:arp-cmd}), + 清空本机已有的ARP缓存, 开启Wireshark,ping本机的同网段地址,在显示过滤器条框中输入“\texttt{arp}”, 观察捕获的ARP报文的各个字段,分析请求/响应的过程。 - \begin{figure}[!ht] - \centering - \begin{code}[text] - arp [-a [InetAddr] [-N IfaceAddr]] [-g [InetAddr] [-N IfaceAddr]] - [-d InetAddr [IfaceAddr]] [-s InetAddr EtherAddr [IfaceAddr]] - - -a 显示所有接口/特定接口的当前 ARP 缓存表 - -g 同-a - -d 删除所有/指定的IP地址项 - -s 在ARP缓存中添加对应InetAddr地址的EtherAddr地址静态项 - \end{code} - \caption{arp命令语法及参数} - \label{fig:arp-cmd} - \end{figure} - \item 使用\texttt{arp –d}命令,清空本机已有的ARP缓存。开启Wireshark, ping本机的不同网段地址或域名,观察捕获的ARP报文的各个字段, 分析请求/响应的过程。 \end{enumerate} +\begin{figure}[!ht] + \centering + \begin{code}[text] + arp [-a [InetAddr] [-N IfaceAddr]] [-g [InetAddr] [-N IfaceAddr]] + [-d InetAddr [IfaceAddr]] [-s InetAddr EtherAddr [IfaceAddr]] + + -a 显示所有接口/特定接口的当前 ARP 缓存表 + -g 同 -a + -d 删除所有/指定的IP地址项 + -s 在ARP缓存中添加对应InetAddr地址的EtherAddr地址静态项 + \end{code} + \caption{arp命令语法及参数} + \label{fig:arp-cmd} +\end{figure} + \subsection{思考题} \label{subsec:c:wireshark:s:arp_rethink} diff --git a/data/cover.tex b/data/cover.tex index d0a8b8d..5999843 100644 --- a/data/cover.tex +++ b/data/cover.tex @@ -48,9 +48,9 @@ \fill[c1] ([xshift=2cm,yshift=2cm]current page.center) rectangle ++ (-13.7,7.7); \node[text=white,anchor=west,scale=4,inner sep=0pt] at -([xshift=-8.55cm,yshift=7cm]current page.center) {计算机网络}; +([xshift=-8.3cm,yshift=7cm]current page.center) {计算机网络}; \node[text=white,anchor=west,scale=2,inner sep=0pt] at -([xshift=-6.2cm,yshift=4.2cm]current page.center) {共同实验指导书}; +([xshift=-4.2cm,yshift=4.2cm]current page.center) {实验指导书}; \node[text=gray,anchor=west,scale=1.5,inner sep=0pt] at ([xshift=-3cm,yshift=-10.5cm]current page.center) {计算机网络教案社区}; diff --git a/data/preface.tex b/data/preface.tex index 165382f..d849068 100644 --- a/data/preface.tex +++ b/data/preface.tex @@ -1,24 +1,32 @@ \begin{pre} - \thispagestyle{empty} -本实验指导书是为计算机网络教案社区发布的课件配套撰写的实验指导书。 -总体上分为基础实验与高级实验两大部分。 -基础实验主要包括网络协议分析与网络程序设计两个单元(共计6个实验), -高级实验主要包括组网基础、路由器设计实现、 -软件定义网络与网络测量等三个单元(共计8个实验), -供有需要的高校选用。 - -考虑到每个学校都有各自的教学大纲和实验环境, -不同学校前序课程不同,开设网络课程的学期也不一致, -社区难以顾及方方面面的需求。 -因此本实验指导书主要是提出了一个实验框架,其中同时包括了基础、进阶和创新内容, -故而命名为“共同”实验。 -在实际使用中, -各位任课老师可以根据具体需求在本指导书的基础上通过 -增删、组合、修改等方法设计合宜的实验方案。 - -本实验指导书由计算机网络教案社区发起, -各高校专家教授共同联合编纂,各章节主要编纂人员如下: +\thispagestyle{empty} +本实验指导书由“计算机网络教案”社区发起, +各高校专家教授共同联合编纂, +汇集了可以配合高校“计算机网络”课程开设的各类实验。 +考虑到高校教学大纲与实验环境各异, +不同学校前序课程不同,开设网络课程的学期也不一致。 +社区尽力采集了不同难度的多种实验, +以配合各高校的实际情况。 + +本指导书中的实验可以分为基础实验与高级实验两大部分。 +其中基础实验主要包括“网络协议分析”与“网络程序设计”两个单元(共计6个实验), +高级实验主要包括“组网基础”、“路由器实现”、 +“软件定义网络与网络测量”三个单元(共计9个实验)。 + +所有实验的目的、任务、要求、难度与建议实验课时数 +在附录\ref{app:cheatsheet}中做了汇总,方便老师快速查询。 +在实际使用中,任课老师可以根据具体授课需要在本指导书的基础上通过 +增删、组合、修改等方法设计适合自身课程的实验方案。 + +由于时间仓促,加之编者水平有限, +本指导书中缺点和疏漏在所难免。 +作为社区项目,恳请广大读者和同行专家批评指正, +同时欢迎更多读者和专家参与本项目的编纂和开发。 + +\vspace{1em} + +本实验指导书各章节主要编纂人员如下: \begin{itemize} \item 第一单元:谢怡、陈建发、洪劼超、雷蕴奇,厦门大学 From 6f16649be4ca88d6511d4482be93cd9249846e0d Mon Sep 17 00:00:00 2001 From: xphi Date: Thu, 25 Feb 2021 11:42:23 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E5=8D=95=E5=85=83=E5=AE=9E=E9=AA=8C=E7=9A=84=E5=8F=82=E8=80=83?= =?UTF-8?q?=E4=BB=A3=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{实验任务与要求:} From d752d299e7d61cd513c2a3f3ebf883e822b6114b Mon Sep 17 00:00:00 2001 From: xphi Date: Thu, 25 Feb 2021 11:44:11 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E8=A7=A3=E5=86=B3code=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CODE/reference/ch_socket/SMTPClient.py | 66 ------------------- CODE/reference/ch_socket/UDP_Pinger_Client.py | 19 ------ CODE/reference/ch_socket/UDP_Pinger_Server.py | 11 ---- CODE/reference/ch_socket/Websever.py | 62 ----------------- CODE/reference/ch_socket/readme.md | 12 ---- 5 files changed, 170 deletions(-) delete mode 100644 CODE/reference/ch_socket/SMTPClient.py delete mode 100644 CODE/reference/ch_socket/UDP_Pinger_Client.py delete mode 100644 CODE/reference/ch_socket/UDP_Pinger_Server.py delete mode 100644 CODE/reference/ch_socket/Websever.py delete mode 100644 CODE/reference/ch_socket/readme.md diff --git a/CODE/reference/ch_socket/SMTPClient.py b/CODE/reference/ch_socket/SMTPClient.py deleted file mode 100644 index bfec76a..0000000 --- a/CODE/reference/ch_socket/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/reference/ch_socket/UDP_Pinger_Client.py b/CODE/reference/ch_socket/UDP_Pinger_Client.py deleted file mode 100644 index db005cf..0000000 --- a/CODE/reference/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/reference/ch_socket/UDP_Pinger_Server.py b/CODE/reference/ch_socket/UDP_Pinger_Server.py deleted file mode 100644 index 25bda49..0000000 --- a/CODE/reference/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/CODE/reference/ch_socket/Websever.py b/CODE/reference/ch_socket/Websever.py deleted file mode 100644 index d901408..0000000 --- a/CODE/reference/ch_socket/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/reference/ch_socket/readme.md b/CODE/reference/ch_socket/readme.md deleted file mode 100644 index addcd4e..0000000 --- a/CODE/reference/ch_socket/readme.md +++ /dev/null @@ -1,12 +0,0 @@ -# 套接字编程参考实现 - -# 说明 - -这个目录中的四个Python文件是实验单元“基于套接字的网络编程” -中三个实验的参考实现代码。 - -其中: - -* UDP_Pinger_*.py 是实验“套接字基础与UDP通信”的参考实现 -* Websever.py 是实验“TCP通信与Web服务器”的参考实现 -* SMTPClient.py 是实验“SMTP客户端实现”的参考实现 \ No newline at end of file From a40174852eb0544e94a5b3ba2a3840566adbb731 Mon Sep 17 00:00:00 2001 From: xphi Date: Thu, 25 Feb 2021 11:44:46 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E8=A7=A3=E5=86=B3code=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/reference/ch_socket/SMTPClient.py | 66 +++++++++++++++++++ code/reference/ch_socket/UDP_Pinger_Client.py | 19 ++++++ code/reference/ch_socket/UDP_Pinger_Server.py | 11 ++++ code/reference/ch_socket/Websever.py | 62 +++++++++++++++++ code/reference/ch_socket/readme.md | 12 ++++ 5 files changed, 170 insertions(+) create mode 100644 code/reference/ch_socket/SMTPClient.py create mode 100644 code/reference/ch_socket/UDP_Pinger_Client.py create mode 100644 code/reference/ch_socket/UDP_Pinger_Server.py create mode 100644 code/reference/ch_socket/Websever.py create mode 100644 code/reference/ch_socket/readme.md diff --git a/code/reference/ch_socket/SMTPClient.py b/code/reference/ch_socket/SMTPClient.py new file mode 100644 index 0000000..bfec76a --- /dev/null +++ b/code/reference/ch_socket/SMTPClient.py @@ -0,0 +1,66 @@ +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/reference/ch_socket/UDP_Pinger_Client.py b/code/reference/ch_socket/UDP_Pinger_Client.py new file mode 100644 index 0000000..db005cf --- /dev/null +++ b/code/reference/ch_socket/UDP_Pinger_Client.py @@ -0,0 +1,19 @@ +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/reference/ch_socket/UDP_Pinger_Server.py b/code/reference/ch_socket/UDP_Pinger_Server.py new file mode 100644 index 0000000..25bda49 --- /dev/null +++ b/code/reference/ch_socket/UDP_Pinger_Server.py @@ -0,0 +1,11 @@ +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/code/reference/ch_socket/Websever.py b/code/reference/ch_socket/Websever.py new file mode 100644 index 0000000..d901408 --- /dev/null +++ b/code/reference/ch_socket/Websever.py @@ -0,0 +1,62 @@ +# 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/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