Question
In this homework, you will practice and reinforce the basics of socket programming for TCP connections in Python: how to create a socket, bind it
In this homework, you will practice and reinforce 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 a HTTP packet. You will also learn some basics of HTTP header format. The requirements for this homework are specified below.
1. Function Requirement: 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 return an HTTP 404 "Not Found" msg.
2. Security Requirement: Note that this Web server will allow its clients to access ANY file that they can name. To prevent strangers from snooping on your files, you should include some basic protections: 1. If the request goes to "/grades/students.html", your server should return an HTTP 403 "Forbidden" response. 2. Additionally, to prevent directory listing, the same 403 message needs to be returned if the request goes to "/grades/" folder.
Skeleton 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. Each place may require one or more lines of code.
Running the Server Put an HTML file (e.g., HelloWorld.html) in the same directory that the server is in. Run the server program. Determine the IP address of the host that is running the server (e.g., 128.238.251.26, or 127.0.0.1 if you run your server program locally on your own device). In either case, you can open up a browser on your client machine and type the following URL.
http://127.0.0.1:6789/HelloWorld.html
where 6789 is the port number you hardwired into your server code and 'HelloWorld.html' is the name of the file you placed in the server directory (case matters!). The browser should then display the contents of HelloWorld.html. Don't forget the port number; if you leave it out the browser will use the default HTTP port 80 and you won't get a result. Then try to get a file that is not present at the server. You should get a 404 "Not Found" message. For security feature testing, you can create a folder named 'grades' inside your server directory and test it in your browser by replacing 'HelloWorld.html' with 'grades/students.html' or 'grades/'.
Skeleton Python Code for the Web Server
# import socket module
from socket import *
import sys # In order to terminate the program
serverSocket = socket ( AF_INET , SOCK_STREAM )
# Prepare a server socket on a particular port
# Fill in code to set up the port
while True :
# Establish the connection
print ('Ready to serve ... ')
connectionSocket , addr = # Fill in code to get a connection
try:
message = # Fill in code to read GET request
filename = message . split () [1]
# Fill in security code
f = open ( filename )
outputdata = # Fill in code to read data from the file
# Send HTTP header line (s) into socket
# Fill in code to send header (s)
# Send the content of the requested file to the client
for i in range (0 , len ( outputdata ) ) :
connectionSocket . send ( outputdata [ i ]. encode () )
connectionSocket . send (" ". encode () )
connectionSocket . close ()
except IOError :
# Send response message for file not found
# Fill in
# Close client socket
# Fill in
serverSocket . close ()
sys . exit () # Terminate the program after sending the corresponding data
______________
What to Hand in You will hand in the complete server code in a python file (tested in Python3) along with a report containing the screenshots of your client browser to verify that you actually receive the contents of the HTML file from the server.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started