ard*_*vis 8 python sockets proxy-server
对于编程练习(来自Kurose和Ross的计算机网络:自上而下的方法(第6版)),我们正在尝试在python中开发一个简单的代理服务器.
我们得到了以下代码,无论它在哪里说#Fill in start. ... #Fill in end.我们需要编写代码.我的具体问题和尝试将在此原始片段下方.
我们需要启动python服务器:python proxyserver.py [server_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.
Run Code Online (Sandbox Code Playgroud)
在哪里说:
# Create a socket on the proxyserver
c = # Fill in start. # Fill in end.
Run Code Online (Sandbox Code Playgroud)
我试过了:
c = socket(AF_INET, SOCK_STREAM)
Run Code Online (Sandbox Code Playgroud)
这似乎是你如何创建一个套接字,然后连接到主机的端口80,我有:
c.connect((hostn, 80))
Run Code Online (Sandbox Code Playgroud)
在这里,根据我所拥有的一些本地印刷报表hostn是正确的google.com.我要填写的下一部分说,#Read response into buffer但我不明白这意味着什么.我认为它与fileobj上面创建的内容有关.
在此先感谢,如果我错过了任何我应该添加的内容,请告诉我.
UPDATE
我现在的代码可以在这里找到我一直在尝试的内容:
https://github.com/ardavis/Computer-Networks/blob/master/Lab%203/ProxyServer.py
这似乎是我的潜在解决方案.来自作业的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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13313 次 |
| 最近记录: |