Lab 4 socket programming (1)
Lab 4 socket programming (1)
This lab has two parts, the first is to introduce socket programming using Python using a client-server
paradigm. The first task can be completed by making reference to lecture 4 notes on the socket
programming task. The second task is the creation of a web proxy server, completion of the first task
will be useful to complete the second task. This lab will continue next week where we will use
Wireshark to capture TCP traffic that we generate using these servers and their clients. Therefore, the
submission link for this lab will open next week.
1. Web Server
In this lab, you will learn the basics of socket programming for TCP connections in Python: how to create
a socket, bind it to a specific address and port, as well as send and receive an HTTP packet. You will also
learn some basics of HTTP header format.
You will develop a web server that handles one HTTP request at a time. Your web server should accept
and parse the HTTP request, get the requested file from the server’s file system, create an HTTP response
message consisting of the requested file preceded by header lines, and then send the response directly to
the client. If the requested file is not present in the server, the server should send an HTTP “404 Not
Found” message back to the client.
Code
Below you will find the skeleton code for the Web server. 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.
‘HelloWorld.html’ is the name of the file you placed in the server directory. Note also the use of the port
number after the colon. You need to replace this port number with whatever port you have used in the
server code. In the above example, we have used the port number 6789. The browser should then display
the contents of HelloWorld.html. If you omit ":6789", the browser will assume port 80 and you will get
the web page from the server only if your server is listening at port 80.
Then try to get a file that is not present on the server. You should get a “404 Not Found” message.
What to Hand in
You will hand in the complete server code along with the screenshots of your client browser, verifying
that you actually receive the contents of the HTML file from the server. If you attempt an additional task also
showcase the output from that task.
#Fill in start
#Fill in end
1|Page
while True:
print('Ready to serve...')
try:
filename = message.split()[1]
f = open(filename[1:])
#Fill in start
#Fill in end
connectionSocket.send(outputdata[i].encode())
connectionSocket.send("\r\n".encode())
connectionSocket.close()
except IOError:
#Fill in start
#Fill in end
#Fill in start
#Fill in end
serverSocket.close()
Additional task
Instead of using a browser, write your own HTTP client to test your server (the class example will be useful
here). Your client will connect to the server using a TCP connection, send an HTTP request to the server, and
display the server
response as an output. You can assume that the HTTP request sent is a GET method.
2|Page
The client should take command line arguments specifying the server IP address or hostname, the
port at which the server is listening, and the path at which the requested object is stored at the server.
The following is an input command format to run the client.
client.py server_host server_port filename
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. 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.
Request Request
Proxy Web
Client
Server Server
Response Response
Code
Below 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.
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.
3|Page
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.
What to Hand in
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.
import sys
if len(sys.argv) <= 1:
sys.exit(2)
# Fill in start.
# Fill in end.
while 1:
print('Ready to serve...')
print(message)
print(message.split()[1])
filename = message.split()[1].partition("/")[2]
4|Page
print(filename)
fileExist = "false"
print(filetouse)
try:
f = open(filetouse[1:], "r")
outputdata = f.readlines()
fileExist = "true"
tcpCliSock.send("Content-Type:text/html\r\n")
# Fill in start.
# Fill in end.
except IOError:
if fileExist == "false":
hostn = filename.replace("www.","",1)
print(hostn)
try:
# Fill in start.
# Fill in end.
5|Page
fileobj = c.makefile('r', 0)
# Fill in start.
# Fill in end.
# Fill in start.
# Fill in end.
except:
print("Illegal request")
else:
# Fill in start.
# Fill in end.
tcpCliSock.close()
# Fill in start.
# Fill in end.
Additional Exercises
1. Currently the proxy server does no error handling. This can be a problem especially when the
client requests an object which is not available, since the "404 Not found" response usually has no
response body and the proxy assumes there is a body and tries to read it.
2. The simple proxy server supports only HTTP GET method. Add support for POST, by including
the request body sent in the POST-request.
3. Caching: A typical proxy server will cache the web pages each time the client makes a particular
request for the first time. Consider how you would implement the caching functionality in this
server.
6|Page