Assignment: Create a Basic HTTP Protocol Library
Objective
The objective of this assignment is to understand the basics of HTTP and socket programming by
creating a simple HTTP server in Python. The server will handle HTTP GET requests and serve static
HTML content.
Requirements
1. Create a Python class SimpleHTTPServer.
2. The server should listen on a specified host and port.
3. The server should handle basic HTTP GET requests.
4. The server should respond with static HTML content.
5. Implement error handling for unsupported HTTP methods.
6. Use multi-threading to handle multiple client connections simultaneously.
Instructions
1. Setting Up the Server:
1. Create a class named SimpleHTTPServer.
2. Initialize a socket to listen for incoming connections on a specified host and
port.
2. Handling Client Connections:
1. Accept incoming connections.
2. Read and parse HTTP requests from the client.
3. Handle HTTP GET requests.
4. Generate and send HTTP responses to the client.
5. Close the client connection.
3. Serving Static Content:
1. Respond with a simple HTML page for GET requests.
4. Error Handling:
1. Respond with a 405 Method Not Allowed status for unsupported HTTP
methods.
5. Multi-threading:
1. Use threading to handle multiple client connections simultaneously.
Example Code Structure
Below is a skeleton code structure to help you get started:
Python
import socket
import threading
import os
import logging
class SimpleHTTPServer:
def __init__(self, host='127.0.0.1', port=8080):
self.host = host
self.port = port
# TODO: Initialize server socket
# TODO: Bind socket to host and port
# TODO: Start listening for connections
# Set up logging
logging.basicConfig(filename='server.log', level=logging.INFO,
format='%(asctime)s - %(message)s')
def start(self):
# TODO: Accept incoming connections and handle each client in a
new thread
pass
def handle_client(self, client_socket):
# TODO: Read the HTTP request from the client socket
# TODO: Log the request
# TODO: Handle the request and generate a response
# TODO: Send the response back to the client
# TODO: Close the client socket
pass
def handle_request(self, request):
# TODO: Parse the HTTP request to extract method and path
# TODO: Handle GET requests and generate responses
# TODO: Handle unsupported methods
pass
def get_response(self, path):
# TODO: Serve static files from the 'static' directory
# TODO: Handle file not found (404 Not Found)
response_body = f"<html><body><h1>Hello, you requested
{path}</h1></body></html>"
response = (
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Content-Length: " + str(len(response_body)) + "\r\n"
"Connection: close\r\n"
"\r\n"
+ response_body
)
return response.encode('utf-8')
def method_not_allowed_response(self):
# TODO: Generate a 405 Method Not Allowed response
response_body = "<html><body><h1>405 Method Not
Allowed</h1></body></html>"
response = (
"HTTP/1.1 405 Method Not Allowed\r\n"
"Content-Type: text/html\r\n"
"Content-Length: " + str(len(response_body)) + "\r\n"
"Connection: close\r\n"
"\r\n"
+ response_body
)
return response.encode('utf-8')
if __name__ == "__main__":
server = SimpleHTTPServer()
server.start()
Tasks
1. Initialize the Server:
1. Complete the __init__ method to initialize the server socket.
2. Bind the socket to the specified host and port.
3. Start listening for incoming connections.
2. Handle Client Connections:
1. Implement the start method to accept incoming connections.
2. Create a new thread for each client connection.
3. Parse HTTP Requests:
1. Implement the handle_client method to read the request from the client
socket.
2. Parse the HTTP request to extract the method and path.
4. Generate HTTP Responses:
1. Implement the handle_request method to handle GET requests and generate
appropriate responses.
2. Implement the get_response method to return a simple HTML response for
GET requests.
3. Implement the method_not_allowed_response method to return a 405
Method Not Allowed response for unsupported methods.
5. Multi-threading:
1. Ensure that the server can handle multiple clients simultaneously using
threading.
Submission
Submit the following:
1. The complete Python script for the SimpleHTTPServer class.
2. A README file explaining how to run the server and test it using a web browser or an
HTTP client tool like curl or Postman.
Bonus
1. Serve Static Files:
1. Extend the server to serve static files (e.g., HTML, CSS, JS) from a directory.
2. Logging:
1. Implement logging to record incoming requests and server responses.
Evaluation Criteria
• Correct implementation of the server initialization and socket setup.
• Proper handling of client connections and HTTP requests.
• Correct generation of HTTP responses.
• Implementation of multi-threading for handling multiple clients.
• Code readability and comments explaining the functionality.
Good luck, and have fun building your basic HTTP protocol library!
This assignment should help students understand the fundamentals of HTTP and socket
programming while giving them hands-on experience in creating a simple HTTP server.