作业-Python代理服务器
对于一个编程练习(,来自Kurose和Ross的《计算机网络:自上而下的方法》(第6版)),我们正在尝试用python开发一个简单的代理服务器 我们得到了下面的代码,无论它在哪里显示作业-Python代理服务器,python,sockets,proxy-server,Python,Sockets,Proxy Server,对于一个编程练习(,来自Kurose和Ross的《计算机网络:自上而下的方法》(第6版)),我们正在尝试用python开发一个简单的代理服务器 我们得到了下面的代码,无论它在哪里显示#填写start#填写结尾。这是我们需要编写代码的地方。我的具体问题和尝试将在这个原始片段的下面 我们需要使用以下命令启动python服务器:python proxyserver.py[server\u ip],然后导航到localhost:8888/google.com,完成后它应该可以工作 from socket
#填写start#填写结尾。
这是我们需要编写代码的地方。我的具体问题和尝试将在这个原始片段的下面
我们需要使用以下命令启动python服务器:python proxyserver.py[server\u ip]
,然后导航到localhost:8888/google.com
,完成后它应该可以工作
from socket import *
import sys
if len(sys.argv) <= 1:
print 'Usage : "python ProxyServer.py server_ip"\n[server_ip : It is the IP Address Of Proxy Server'
sys.exit(2)
# 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 client
print '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 message print message.split()[1]
filename = message.split()[1].partition("/")[2] print filename
fileExist = "false"
filetouse = "/" + filename
print filetouse
try:
# Check wether the file exist in the cache
f = open(filetouse[1:], "r")
outputdata = f.readlines()
fileExist = "true"
# ProxyServer finds a cache hit and generates a response message
tcpCliSock.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 cache
except IOError:
if fileExist == "false":
# Create a socket on the proxyserver
c = # 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 client
fileobj = 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 cache
tmpFile = 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.
我试过:
c = socket(AF_INET, SOCK_STREAM)
这似乎是您创建套接字的方式,然后为了连接到主机的端口80,我有:
c.connect((hostn, 80))
在这里,hotn
是正确的google.com
,根据我在当地的一些打印声明。我要填写的下一部分是to#将响应读入缓冲区
,但我真的不明白这意味着什么。我猜想它与上面创建的fileobj
有关
提前谢谢,如果我遗漏了任何需要补充的内容,请告诉我
更新
可以在此处找到我当前的代码,以查看我一直在尝试的内容:
这似乎是我的潜在解决方案。作业中的pdf提到我需要在最后做些什么,不确定是什么。但是缓存和代理似乎与此相关。我希望它能帮助别人
from socket import *
import sys
if len(sys.argv) <= 1:
print 'Usage: "python ProxyServer.py server_ip"\n[server_ip : It is the IP Address of the Proxy Server'
sys.exit(2)
# Create a server socket, bind it to a port and start listening
tcpSerPort = 8888
tcpSerSock = socket(AF_INET, SOCK_STREAM)
# Prepare a server socket
tcpSerSock.bind(('', tcpSerPort))
tcpSerSock.listen(5)
while True:
# Start receiving data from the client
print 'Ready to serve...'
tcpCliSock, addr = tcpSerSock.accept()
print 'Received a connection from: ', addr
message = tcpCliSock.recv(1024)
# Extract the filename from the given message
print message.split()[1]
filename = message.split()[1].partition("/")[2]
fileExist = "false"
filetouse = "/" + filename
try:
# Check whether the file exists in the cache
f = open(filetouse[1:], "r")
outputdata = f.readlines()
fileExist = "true"
print 'File Exists!'
# ProxyServer finds a cache hit and generates a response message
tcpCliSock.send("HTTP/1.0 200 OK\r\n")
tcpCliSock.send("Content-Type:text/html\r\n")
# Send the content of the requested file to the client
for i in range(0, len(outputdata)):
tcpCliSock.send(outputdata[i])
print 'Read from cache'
# Error handling for file not found in cache
except IOError:
print 'File Exist: ', fileExist
if fileExist == "false":
# Create a socket on the proxyserver
print 'Creating socket on proxyserver'
c = socket(AF_INET, SOCK_STREAM)
hostn = filename.replace("www.", "", 1)
print 'Host Name: ', hostn
try:
# Connect to the socket to port 80
c.connect((hostn, 80))
print 'Socket connected to port 80 of the host'
# Create a temporary file on this socket and ask port 80
# for the file requested by the client
fileobj = c.makefile('r', 0)
fileobj.write("GET " + "http://" + filename + " HTTP/1.0\n\n")
# Read the response into buffer
buff = fileobj.readlines()
# 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
tmpFile = open("./" + filename, "wb")
for i in range(0, len(buff)):
tmpFile.write(buff[i])
tcpCliSock.send(buff[i])
except:
print 'Illegal request'
else:
# HTTP response message for file not found
# Do stuff here
print 'File Not Found...Stupid Andy'
a = 2
# Close the socket and the server sockets
tcpCliSock.close()
# Do stuff here
从套接字导入*
导入系统
如果len(sys.argv)仅供参考,socket.create\u connection
更容易。我以前从未使用过python,所以我还有很多东西要学。我会看一看,我只知道我们必须按计划完成任务。谢谢嘿,我收到了一个非正式的投诉-如果你能记得练习代码的最初来源,那么为练习代码提供属性可能是个好主意。完成了,谢谢你的提示。很抱歉。
from socket import *
import sys
if len(sys.argv) <= 1:
print 'Usage: "python ProxyServer.py server_ip"\n[server_ip : It is the IP Address of the Proxy Server'
sys.exit(2)
# Create a server socket, bind it to a port and start listening
tcpSerPort = 8888
tcpSerSock = socket(AF_INET, SOCK_STREAM)
# Prepare a server socket
tcpSerSock.bind(('', tcpSerPort))
tcpSerSock.listen(5)
while True:
# Start receiving data from the client
print 'Ready to serve...'
tcpCliSock, addr = tcpSerSock.accept()
print 'Received a connection from: ', addr
message = tcpCliSock.recv(1024)
# Extract the filename from the given message
print message.split()[1]
filename = message.split()[1].partition("/")[2]
fileExist = "false"
filetouse = "/" + filename
try:
# Check whether the file exists in the cache
f = open(filetouse[1:], "r")
outputdata = f.readlines()
fileExist = "true"
print 'File Exists!'
# ProxyServer finds a cache hit and generates a response message
tcpCliSock.send("HTTP/1.0 200 OK\r\n")
tcpCliSock.send("Content-Type:text/html\r\n")
# Send the content of the requested file to the client
for i in range(0, len(outputdata)):
tcpCliSock.send(outputdata[i])
print 'Read from cache'
# Error handling for file not found in cache
except IOError:
print 'File Exist: ', fileExist
if fileExist == "false":
# Create a socket on the proxyserver
print 'Creating socket on proxyserver'
c = socket(AF_INET, SOCK_STREAM)
hostn = filename.replace("www.", "", 1)
print 'Host Name: ', hostn
try:
# Connect to the socket to port 80
c.connect((hostn, 80))
print 'Socket connected to port 80 of the host'
# Create a temporary file on this socket and ask port 80
# for the file requested by the client
fileobj = c.makefile('r', 0)
fileobj.write("GET " + "http://" + filename + " HTTP/1.0\n\n")
# Read the response into buffer
buff = fileobj.readlines()
# 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
tmpFile = open("./" + filename, "wb")
for i in range(0, len(buff)):
tmpFile.write(buff[i])
tcpCliSock.send(buff[i])
except:
print 'Illegal request'
else:
# HTTP response message for file not found
# Do stuff here
print 'File Not Found...Stupid Andy'
a = 2
# Close the socket and the server sockets
tcpCliSock.close()
# Do stuff here