Skip to content

Latest commit

 

History

History
119 lines (98 loc) · 2.24 KB

File metadata and controls

119 lines (98 loc) · 2.24 KB

Python HTTP Server

A minimal HTTP server built from scratch in Python using the built-in socket module.

The goal of this project was to understand how HTTP communication works underneath frameworks like FastAPI, Flask, and Express by implementing the basic client-server communication manually.

🚀 Features

  • Creates a TCP socket using Python's socket module
  • Binds the server to a host and port
  • Listens for incoming TCP connections
  • Accepts client connections
  • Receives and parses basic HTTP requests
  • Supports the GET method
  • Serves HTML files
  • Serves JSON data
  • Returns a custom 404 page for unknown routes
  • Returns 405 Method Not Allowed for unsupported HTTP methods

🛠️ Technologies

  • Python 3
  • TCP Sockets
  • HTTP/1.1
  • HTML
  • JSON

📁 Project Structure

python-http-server/
│
├── main.py
├── index.html
├── data.json
├── not_found.html
└── README.md

How It Works

The server uses Python's socket module to communicate directly with clients over TCP.

The basic flow is:

Client / Browser
       ↓
TCP Connection
       ↓
socket.accept()
       ↓
Receive HTTP Request
       ↓
Parse HTTP Method & Path
       ↓
Determine Resource
       ↓
Build HTTP Response
       ↓
Send Response
       ↓
Close Connection

Example Request

When visiting:

http://localhost:8080/

the browser sends an HTTP request similar to:

GET / HTTP/1.1
Host: localhost:8080

The server extracts:

Method → GET
Path   → /

and serves index.html.

Similarly:

/data

serves data.json.

Any unknown path serves not_found.html.

▶️ Running the Server

Make sure Python is installed.

Clone the repository:

git clone https://github.com/itisrudraa/python-http-server.git
cd python-http-server

Run the server:

python main.py

You should see:

Listening to 8080 ...

Then open:

http://localhost:8080

in your browser.

🎯 Purpose

The main purpose of this project is learning.

Instead of starting with a framework and hiding the networking layer behind abstractions, this project explores what happens underneath a basic web server.

Built from scratch to understand the fundamentals of HTTP and backend development.