EE502 W5 Programming
EE502 W5 Programming
Web Proxy
Objectives
In this lab, you will implement a small web proxy server that is able to cache web pages. It
is a simple proxy server that only understands simple GET-requests, but is able to handle all
kinds of objects—not just HTML pages, but also images.
Introduction
Generally, when the client makes a request, the request is sent to the web server. The web
server then processes the request and sends back a response message to the requesting
client. In order to improve the performance we create a proxy server between the client and
the web server, as shown in Figure 1.
Figure 1
Now, both the request message sent by the client and the response message delivered by
the web server pass through the proxy server. In other words, the client requests the
objects via the proxy server. The proxy server will forward the client’s request to the web
server. The web server will then generate a response message and deliver it to the proxy
server, which in turn sends it to the client.
Equipment List
A PC or laptop installed with Python 2.7.9 connected to the Internet.
Procedure
1. In Figure 1, you will find the skeleton code for the client. You are to complete the
skeleton code. The places where you need to fill in code are marked with #Fill in
start and #Fill in end. Each place may require one or more lines of code. You
may follow the program as shown in Figure 2.
Skeleton Python Code for the Proxy Server
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 whether 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.
if len(sys.argv ) <= 1:
tcpSerSock.bind((sys.argv[1], 6788))
tcpSerSock.listen(100)
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(1024)
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")
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:
if fileExist == "false":
# Create a socket on the proxyserver
c = socket(AF_INET, SOCK_STREAM)
hostn = filename.replace("www.","",1)
print hostn
try:
# Connect to the socket to port 80
c.connect((hostn, 80))
# 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 line in buff:
tmpFile.write(line);
tcpCliSock.send(line);
except:
print "Illegal request"
else:
# HTTP response message for file not found
tcpCliSock.send("HTTP/1.0 404 sendErrorErrorError\r\n")
tcpCliSock.send("Content-Type:text/html\r\n")
tcpCliSock.send("\r\n")
# Close the client and the server sockets
tcpCliSock.close()
tcpSerSock.close()
3. To use the proxy server with browser and proxy on separate computers, you will
need the IP address on which your proxy server is running. In this case, while
running the proxy, you will have to replace the localhost with the IP address of the
computer where the proxy server is running. Also, note the port number used.
You will replace the port number used here 8888 with the port number you have
used in your server code, at which your proxy server is listening.
Figure 3
4. You can also directly configure your web browser to use your proxy. This depends
on your browser. In Internet Explorer, you can set the proxy in Tools > Internet
Options > Connections tab > LAN Settings. In Netscape (and derived browsers,
such as Mozilla), you can set the proxy in Tools > Options > Advanced tab >
Network tab > Connection Settings. In both cases, you need to give the address
of the proxy and the port number that you gave when you ran the proxy server.
You should be able to run the proxy and the browser on the same computer
without any problem. With this approach, to get a web page using the proxy
server, you simply provide the URL of the page you want.
5. You will hand in the complete proxy server code and screenshots at the client side
verifying that you indeed get the web page via the proxy server.
Programming Assignment Report—Due at the end of Week 5
Write a basic report commenting on the above API programming experiment. The report must
include a cover page with the lab title, the course number, your student name, and your DeVry
DSI number. You must also include a basic introduction that addresses the purpose of this
programming lab. Students must also include basic background information on how the
experiment was set up. Summarize key findings. What were some of the challenges, if any, that
you faced in completing this assignment? How did you overcome those challenges? What
possible improvement can you make in this assignment?