首页 > 编程知识 正文

《计算机网络自顶向下》Socket Lab4 HTTP Web Proxy Server

时间:2023-05-05 20:02:30 阅读:116349 作者:58

感谢文章目录相关代码的链接,在提供详细错误想法之前,请参考Lab4 HTTP Web Proxy ServerLab4文档,修改服务器框架代码注意事项(容易出错)的核心代码Lab4是Lab的实验服务器代理打开服务器代理)缓存访问(缓存访问)建议您缓存访问)

感谢您为相关代码链接提供详细的错误想法

Computer-Networking-A-Top-Down-Approach-NOTES GitHub 套接字编程作业 + Wireshark实验

前引说实话 这个Lab 昨晚拿到手的时候 我都觉得还不困难 但为什么直到今天下午的六点钟(虽然睡了一上午+去核酸检测耽误了很久)才做出来 根本就是刚开始有思路却对全局的实现 没有一个清晰的认识以及一些细节的把控导致错误频出

趁刚刚才把Lab做出来 我还是想一点点抽丝剥解的把这个Lab的实现流程给写出来吧 优质的代码在上面我已经提供链接了 之后的实验如果有些地方有问题的话 大家没思路可以去看看上面的代码 还是很真心感谢上面的代码 在一些地方我看了才得以矫正 不然的话- - 不知道那些细节又要耽误多久

la B4 http web代理服务器la B4文档http://www.Sina.com/http://www.Sina.com /

方便大家下载相关文档 下面是下载链接 中文翻译找到Python3 socket即可

http://www.Sina.com/http://www.Sina.com /

服务器框架代码fromsocketimport * importsys # createaserversocket,bindittoaportandstartlisteningtcpsersock=socket (af _ ii sock _ stream (# fillin start.# fillin end.while 1: # stratreceivingdatafromtheclientprint ) ' readytoserve . ' ) tcpcce addr=tcpSerSock.accept () print ) receivedaconectionfon addr (消息=# fillin start.# fillin end.print )消息) # extractthefilenamefromthegivenmessageprint ) message.spl iint filename=message.split ((1).partition ) '/' )2) PPP ' filet ouse=checkwetherthefileexistinthecachef=open (filet ouse [ 1: ], ' r ' ) output data=f.read lines (文件exist=' true ' # proxyserverfindsachehitandgeneratesaresponsemessagetcpclisock n ' ) htmlr(n ' ) # fillin start.# fillin end.print (' readfromcache ' ) )。 errorhandlingforfilenotfoundincacheexceptioerror 3360 if file exist==' false ' : # createasocketontheproxyserverc=# fill fillin end.hostn=filename.replace (' www.',',1 ) print(hostn ) try : # connecttothesockettoport 80 # fillilint

file('r', 0) fileobj.write("GET "+"http://" + filename + " HTTP/1.0nn") # 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 listeningtcpSerSock = socket(AF_INET, SOCK_STREAM)tcpSerSock.bind(('0.0.0.0',2333))tcpSerSock.listen(5)while 1: # Strat receiving data from the client print('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 = False filepath = "D:/Love6 html cache/" + requestWeb + '_' + filename # Check whether the file exist in the cache try: f = open(filepath, "r") print("file Exist") outputdata = f.read() fileExist = True # ProxyServer finds a cache hit and generates a response message tcpCliSock.send(outputdata.encode()) print('Read from cache') f.close() # Error handling for file not found in cache except IOError: if fileExist == False: # Create a socket on the proxyserver print("file Not Exist"+ '___' + requestWeb) c = socket(AF_INET, SOCK_STREAM) requestPort = 80 try: # Connect to the socket to port 80 c.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 cache tcpCliSock.send(retSentence.encode()) print(filepath) f = open(filepath, "w") f.write(retSentence.replace('rn', 'n')) f.close() except: print("Illegal request") c.close() else: # HTTP response message for file not found tcpCliSock.send("HTTP/1.0 404 Not Foundrn".encode()) tcpCliSock.send("rn".encode()) tcpCliSock.send("404 Not Found Cant Find such file " + filename +"rn".encode()) # Close the client and the server sockets tcpCliSock.close()tcpSerSock.close() Lab4 建议Lab实验成果 直连链接

没有增加服务器代码 直连结果
http://gaia.cs.umass.edu/wireshark-labs/INTRO-wireshark-file1.html

增加服务器代理

我们下面增加服务器代理
打开控制面板->网络和共享中心->左下角internet 属性

设置成如下的情况 增加即可

打开服务器代理(进行访问 进行缓存)

我们打开代理后 各种网络访问就全来了 我们在进行网络访问之后 就寻找一下我们的请求在哪里 如下

在看到之后 我们再看看浏览器中是否正确显示

打开服务器代理(进行访问 已缓存)

我们去我们的存储路径看看文件是否存储进来了 嗯 储存进来了

并且我们再次访问 再看看代理服务器的响应 如下 显示 哦 已经读到了缓存 再去看看网页显示

显示成功!

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。