文章目录
-
- 感谢相关代码链接 提供细节出错思路
- 前引
- Lab4 HTTP Web Proxy Server
-
- Lab4 文档查阅
- 服务器框架代码
- 注意事项(易错点)
- 修改完的核心代码
- Lab4 建议Lab实验成果
-
- 直连链接
- 增加服务器代理
- 打开服务器代理(进行访问 进行缓存)
- 打开服务器代理(进行访问 已缓存)
感谢相关代码链接 提供细节出错思路
Computer-Networking-A-Top-Down-Approach-NOTES GitHub 套接字编程作业 + Wireshark实验
前引
说实话 这个Lab 昨晚拿到手的时候 我都觉得还不困难 但为什么直到今天下午的六点钟(虽然睡了一上午+去核酸检测耽误了很久)才做出来 根本就是刚开始有思路却对全局的实现 没有一个清晰的认识以及一些细节的把控导致错误频出
趁刚刚才把Lab做出来 我还是想一点点抽丝剥解的把这个Lab的实现流程给写出来吧 优质的代码在上面我已经提供链接了 之后的实验如果有些地方有问题的话 大家没思路可以去看看上面的代码 还是很真心感谢上面的代码 在一些地方我看了才得以矫正 不然的话- - 不知道那些细节又要耽误多久
Lab4 HTTP Web Proxy Server
Lab4 文档查阅
方便大家下载相关文档 下面是下载链接 中文翻译找到Python3 socket
即可
Pearson Computer Networking: a Top-Down Approach, 8th Edition
看看中文翻译 由于书本上的我们任务时强调 多线程调度 而实验中的是强调我们把缓存给整明白了 我们大致浏览了一下就可以先把思路给理一下
1、我们需要设置代理服务器 我们所有的网页访问都经由代理服务器
2、我们主要是要把缓存给弄好 即如果访问过的文件 即直接返回即可(当然这是很简单的缓存 就不涉及修改时间那些的 如果有缓存的话直接返回即可)
服务器框架代码
from socket import *
import sys
# Create a server socket, bind it to a port and start listening
tcpSerSock = socket(AF_INET, SOCK_STREAM)
# Fill in start.
# Fill in end.
while 1:# Strat receiving data from the clientprint('Ready to serve...')tcpCliSock, addr = tcpSerSock.accept()print('Received a connection from:', addr)message = # Fill in start. # Fill in end.print(message)# Extract the filename from the given messageprint(message.split()[1])filename = message.split()[1].partition("/")[2]print(filename)fileExist = "false"filetouse = "/" + filenameprint(filetouse)try:# Check wether the file exist in the cachef = open(filetouse[1:], "r") outputdata = f.readlines() fileExist = "true"# ProxyServer finds a cache hit and generates a response messagetcpCliSock.send("HTTP/1.0 200 OK\r\n") tcpCliSock.send("Content-Type:text/html\r\n")# Fill in start.# Fill in end.print('Read from cache') # Error handling for file not found in cacheexcept IOError:if fileExist == "false": # Create a socket on the proxyserverc = # Fill in start. # Fill in end.hostn = filename.replace("www.","",1) print(hostn) try:# Connect to the socket to port 80# Fill in start.# Fill in end.# Create a temporary file on this socket and ask port 80 for the file requested by the clientfileobj = c.makefile('r', 0) fileobj.write("GET "+"http://" + filename + " HTTP/1.0\n\n") # Read the response into buffer# Fill in start.# Fill in end.# Create a new file in the cache for the requested file. # Also send the response in the buffer to client socket and the corresponding file in the cachetmpFile = open("./" + filename,"wb") # Fill in start.# Fill in end.except:print("Illegal request") else:# HTTP response message for file not found# Fill in start.# Fill in end.# Close the client and the server sockets tcpCliSock.close() # Fill in start.# Fill in end.
注意事项(易错点)
其实这个Lab你说难吧 又不难 思路很清晰 但是呢 一行行代码写下来 还是很容易出错的 所以建议写一步检查一步
下面一些点是我写的时候出错检查了很久的地方
1、windows中 存储/r/n
再读取时会导致第一行出错 并且会多打一行
所以我们在存入的时候需要把/r/n
给替换成/r
如果不这样处理 返回时 第一条HTTP/1.1
200
OK就被吞了
2、文件打开处理 同一个文件打开用同一个变量 我反正用其他变量 就会报错 不知道为什么
3、文字处理数目 尽量大一点 我设置为1024的时候会报错 然后就设置成4096了
4、缓存路径存储 记得把文件的/
替换成_
或者其他符号 因为这个在路径中 寻找时或者创建时 会变成下一层路径寻址 就会出错
其他的就是路径设置了 和一些python语法 函数问题了 大家注意一下即可
当然 原版的框架我也做了很多修改
修改完的核心代码
下面就直接改代码了 如果大家自己的代码哪里出问题了 可以看一下下面的代码作为参照
from socket import *
import sys
# Create a server socket, bind it to a port and start listening
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(('0.0.0.0',2333))
tcpSerSock.listen(5)
while 1:# Strat receiving data from the clientprint('Ready to serve...')tcpCliSock, addr = tcpSerSock.accept()print('Received a connection from:', addr)message = tcpCliSock.recv(4096).decode()allpath = message.split()[1].partition("//")[2]requestWeb = allpath.partition('/')[0]filename = allpath.partition("/")[2].replace('/','_')fileExist = Falsefilepath = "D:/Love6 html cache/" + requestWeb + '_' + filename# Check whether the file exist in the cachetry:f = open(filepath, "r")print("file Exist")outputdata = f.read()fileExist = True# ProxyServer finds a cache hit and generates a response messagetcpCliSock.send(outputdata.encode())print('Read from cache')f.close()# Error handling for file not found in cacheexcept IOError:if fileExist == False:# Create a socket on the proxyserverprint("file Not Exist"+ '___' + requestWeb)c = socket(AF_INET, SOCK_STREAM)requestPort = 80try:# Connect to the socket to port 80c.connect((requestWeb,requestPort))c.send(message.encode())retSentence = c.recv(4096).decode()# Create a new file in the cache for the requested file.# Also send the response in the buffer to client socket and the corresponding file in the cachetcpCliSock.send(retSentence.encode())print(filepath)f = open(filepath, "w")f.write(retSentence.replace('\r\n', '\n'))f.close()except:print("Illegal request")c.close()else:# HTTP response message for file not foundtcpCliSock.send("HTTP/1.0 404 Not Found\r\n".encode())tcpCliSock.send("\r\n".encode())tcpCliSock.send("404 Not Found Cant Find such file " + filename +"\r\n".encode())# Close the client and the server socketstcpCliSock.close()
tcpSerSock.close()
Lab4 建议Lab实验成果
直连链接
没有增加服务器代码 直连结果
http://gaia.cs.umass.edu/wireshark-labs/INTRO-wireshark-file1.html
增加服务器代理
我们下面增加服务器代理
打开控制面板->网络和共享中心->左下角internet 属性
设置成如下的情况 增加即可
打开服务器代理(进行访问 进行缓存)
我们打开代理后 各种网络访问就全来了 我们在进行网络访问之后 就寻找一下我们的请求在哪里 如下
在看到之后 我们再看看浏览器中是否正确显示
打开服务器代理(进行访问 已缓存)
我们去我们的存储路径看看文件是否存储进来了 嗯 储存进来了
并且我们再次访问 再看看代理服务器的响应 如下 显示 哦 已经读到了缓存 再去看看网页显示
显示成功!