Skip to content

Aspirant200715/HTTP-Server-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ MiniExpress – A Python HTTP Server From Scratch A lightweight Express.js-style HTTP framework built using only Python sockets. .

πŸ“Œ Overview

This project implements a complete HTTP/1.1 web server from scratch using Python’s standard library only. No frameworks like Flask, Django, Express, FastAPI, or high-level HTTP libraries were used.

It provides:

Routing (GET, POST, DELETE, etc.)

Query parameters & path parameters

Static file serving

JSON request parsing

In-memory data storage

CORS support

Basic logging middleware

Express.js–style API (app.get(), app.post(), …)

This server is built fully manually using:

socket

threading

json

re It demonstrates understanding of low-level HTTP, server architecture, and socket programming.

πŸ“‚ Project Structure project/ β”‚ β”œβ”€β”€ server.py # Custom MiniExpress framework (core server) β”œβ”€β”€ app.py # Your application routes β”œβ”€β”€ static/ # Public static files served under /static β”‚ └── index.html └── README.md

🧠 How It Works (Beginner Friendly)

  1. Sockets

The server manually creates a TCP socket:

socket.socket(socket.AF_INET, socket.SOCK_STREAM)

It binds to:

0.0.0.0:8080

and waits for incoming connections.

  1. HTTP Request Parsing

Each incoming HTTP request is manually parsed:

Request line β†’ GET /path HTTP/1.1

Headers β†’ Host, Content-Length, Content-Type, etc.

Body β†’ Parsed for POST/PUT requests

Query parameters β†’ ?message=hello

Path parameters β†’ /user/:id

  1. Routing System

You register routes just like Express.js:

@app.get("/echo") @app.post("/data") @app.get("/data/:id")

The server internally converts paths like /data/:id into regex and extracts parameters.

  1. Threaded Handling

Each request is processed in a separate thread for concurrency.

  1. Response Building

Every HTTP response is manually constructed:

HTTP/1.1 200 OK Content-Type: application/json Content-Length: 27 Access-Control-Allow-Origin: *

πŸš€ How to Run the Server

  1. Install Python 3

Ensure Python 3.8+ is installed.

  1. Run the server

In the terminal:

python3 app.py

You will see:

MiniExpress listening on port 8080

Your server is now live at:

πŸ‘‰ http://localhost:8080

πŸ§ͺ API Endpoints βœ” GET /

Returns welcome message

Request

GET /

Response

Welcome to MiniExpress!

βœ” GET /echo?message=hello

Echoes the query param.

Response

Echo: hello

βœ” GET /user/:id

Example:

GET /user/10

Response:

{"user_id": "10"}

βœ” POST /data

Stores JSON in memory.

Example Request

POST /data Content-Type: application/json

{"name":"Soham","value":123}

Response

{"id": 1}

βœ” GET /data

Returns all records.

[ {"id":1,"data":{"name":"Soham","value":123}} ]

βœ” GET /data/:id GET /data/1

Response

{"id": 1, "data": {"name":"Soham","value":123}}

If not found β†’ 404 Not Found

πŸ“ Static File Serving

Any file placed in:

./static

is served from URL:

/static/

Example:

static/index.html β†’ http://localhost:8080/static/index.html

πŸ›  Bonus Features Implemented βœ” 1. Request Logging Middleware

Every request prints:

('127.0.0.1', 42022) - GET /data

βœ” 2. Static File Serving

Automatically serves files under /static.

βœ” 3. CORS Support

All responses include:

Access-Control-Allow-Origin: *

🧱 Design Decisions Why build from scratch?

To understand:

How browsers communicate with servers

How HTTP requests look before frameworks parse them

How routing works internally

How to build low-level I/O logic

How modern frameworks (Express/Flask/FastAPI) work internally

Why threading?

The assignment needs ability to handle many simultaneous connections.

Why in-memory storage?

Meet the requirement: β€œstore data in RAM”

Regex-based routing

Used to support:

/user/:id /data/:id

πŸ§ͺ Test Using cURL Home: curl http://localhost:8080/

Echo: curl "http://localhost:8080/echo?message=hello"

POST data: curl -X POST http://localhost:8080/data
-H "Content-Type: application/json"
-d '{"name":"Soham","value":123}'

GET all data: curl http://localhost:8080/data

πŸ“Œ Limitations Data is not persistent (RAM only)

No HTTPS support

Single-thread performance may vary

No authentication (can be added)

os

It demonstrates understanding of low-level HTTP, server architecture, and socket programming.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published