Skip to content

Lab02: The Application Layer Part 2 -- HTTP and Ping Applications

Learning Goals

Goals for this week's lab

  • Implement a simple web server application that can respond to HTTP requests by retrieving and sending files from the server's file system.
  • Implement a simple UDP pinger client, which will interact with the server and provide statistics for said communication, such as minimum, maximum, and average RTT.
  • Study the effects of packet loss.

General notes

You can execute all Python programs locally on your own machines, or copy your programs into the nodes running within GNS3 on your lab VM (the latter requires setting up IP addresses first).

We suggest using your lab VM: When connected to the VM, you can navigate to the lab02 directory and type python3 programName.py or ./programName.py to run the Python program from the file programName.py in the current working directory. If your server program would like to listen to ports in the range 1-1023 (“privileged ports”), you will need to run your program with elevated rights using sudo python3 programName.py or sudo ./programName.py instead.

Example programs from the lectures

Exercises adjusted from Computer Networking: A Top-Down Approach 9th ed.


Lab Exercises

A) Web Server

In this task, 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 HTTP packets.
  • Some basics of the 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

In the lab02 directory on your VM, you will find the skeleton code for the web server in WebServer_Skeleton.py. You are to complete the skeleton code.

The places where you need to fill in code are marked with ## TODO fill in start and ## TODO fill in end. Each place may require one or more lines of code.

Running the Server

Make sure that there is an HTML file (e.g. HelloWorld.html) in the same directory that the server is in, and then run the server program. Determine the IP address of the host that is running the server (for instance your lab VM 10.212.XXX.YYY).

From another host (e.g., your personal machine or one of the PCs in Sahara), open a browser and provide the corresponding URL. For example:

http://10.212.XXX.YYY:6789/HelloWorld.html
  • 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 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 at the server. You should get a 404 Not Found message.

REPORT

Include screenshots of both successful and not successful requests to your lab report and attach the completed WebServer.py to your submission for lab02.


B) UDP Pinger

In this task, you will learn the basics of socket programming for UDP in Python:

  • How to send and receive datagrams using UDP sockets.
  • How to set a proper socket timeout.

Further, you will gain familiarity with a ping application and its usefulness in computing statistics such as packet loss rate.

Firstly, you will study a simple ping server written in Python and implement a corresponding client. The functionality provided by these programs is similar to the functionality provided by standard ping programs available in modern operating systems. However, these programs use a simpler protocol, UDP, rather than the standard Internet Control Message Protocol (ICMP) to communicate with each other. The ping protocol allows a client machine to send a packet of data to a remote machine, and have the remote machine return the data back to the client unchanged (echoing). Among other uses, the ping protocol allows hosts to determine round-trip times to other machines.

You are given the complete code for the ping server in the file UDPPingerServer.py. Your task is to write the corresponding ping client. The server code fully implements a ping server. You do not need to modify the server code.

Note

In the server code, 30% of the client’s packets are simulated to be lost. You should study this code carefully, as it will help you write your ping client.

Packet Loss

UDP provides applications with an unreliable transport service. Messages may get lost in the network due to router queue overflows, faulty hardware, or other reasons. Because packet loss is rare or even non-existent in typical campus networks, the server in this task injects artificial loss to simulate the effects of network packet loss. The server creates a variable randomized integer which determines whether a particular incoming packet is lost or not.

Client Code

You will implement the following client program.

The client should send 10 pings to the server. Because UDP is an unreliable protocol, a packet sent from the client to the server may be lost in the network, or vice versa. For this reason, the client cannot wait indefinitely for a reply to a ping message. You should let the client wait up to one second for a reply. If no reply is received within one second, your client program should assume that the packet was lost during transmission across the network. You will need to look up the Python documentation to find out how to set the timeout value on a datagram socket.

Specifically, your client program should:

  1. Send the ping message using UDP.

  2. Print the response message from server, if any.

  3. Calculate and print the round trip time (RTT), in milliseconds, of each packet, if the server responds.

  4. Otherwise, print “Request timed out”

During development, it might be easier to run the UDPPingerServer.py on the same machine as your client and test your client by sending packets to 127.0.0.1 (localhost). After you have fully debugged your code, you should see how your application communicates across the network with the ping server and ping client running on different machines.

Message Format

The ping messages in this lab are formatted in a simple way. The client message is one line, consisting of ASCII characters in the following format:

ping sequence_number time
  • sequence_number: starts at 1 and progresses to 10 for each successive ping message sent by the client.
  • time: is the time when the client sent the message.

Adding Statistics

Currently, the program calculates the round-trip time for each packet and prints it out individually. Modify this to correspond to the way the standard ping program works. You will need to report the minimum, maximum, and average RTTs at the end of all pings from the client. In addition, calculate the current packet loss rate as a percentage.

REPORT

In addition to submitting the final version of your UDPPingerClient.py, discuss how the RTT changes when running both server and client on the same machine (for instance your lab VM) vs. on different machines (for instance client on Sahara PC and server on your lab VM)

Remember to use a server IP address of 127.0.0.1 (localhost) when both client and server run on the same machine, and the correct server IP address when they run on different machines.

Finally, run your ping program for 100 instead of 10 iterations and confirm the packet loss rate that is emulated by the server.