Skip to content

This project explores three different ways to serve web content using pure Python—no frameworks. Each server demonstrates a different backend architecture style: CGI Calculator, WSGI Book Catalog, Socket HTTP Server.

License

Notifications You must be signed in to change notification settings

umckinney/three-web-servers-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧮 Python Web Servers: CGI, WSGI & Socket

Python License CI Last Commit

Overview

This project explores three different ways to serve web content using pure Python—no frameworks. Each server demonstrates a different backend architecture style:

  1. CGI Calculator – Traditional Common Gateway Interface server
  2. WSGI Book Catalog – Web Server Gateway Interface using TinyDB
  3. Socket HTTP Server – Raw socket implementation of an HTTP server

Ideal for learning fundamental web backend mechanics and Python networking.


📁 Project Structure

.
├── cgi/                # CGI calculator
│   └── cgi-bin/
│       └── cgi_calc.py
├── wsgi/               # Book catalog using WSGI + TinyDB
│   ├── main.py
│   ├── initial_books.json
│   └── books_db.json
├── simple_server/      # HTTP server using sockets
│   ├── http_server.py
│   ├── tests.py
│   ├── unit-tests.py
│   └── webroot/
│       ├── a_web_page.html
│       └── images/
└── README.md

🔢 Part 1: CGI Calculator

A web-based calculator implemented with CGI. Accepts operands and operations via query string:

Example usage:

http://127.0.0.1:8000/cgi-bin/cgi_calc.py?user=jdoe&operand=11&operand=7&operation=div

Run:

cd cgi
python -m http.server --cgi

📚 Part 2: WSGI Book Catalog

A WSGI server using TinyDB to manage a searchable book catalog. Users can:

  • View all books
  • Add new books
  • Search by title, author, and year

Example search queries:

http://localhost:8000/books/search?title=1984
http://localhost:8000/books/search?year=1925&title=The%20Great%20Gatsby

Run:

cd wsgi
python -m venv .venv && source .venv/bin/activate
pip install tinydb
python main.py

🌐 Part 3: Raw Socket HTTP Server

Implements an HTTP 1.1 server using Python sockets. Supports:

  • Directory browsing
  • MIME-type detection
  • Static file serving (HTML, text, images, etc.)

Run:

cd simple_server
python -u http_server.py

Then visit:


✅ Tests

The socket server includes basic tests using unittest:

python unit-tests.py
python tests.py

📌 Note: You may need to add a delay in the test setup to ensure the server boots before tests run.


🧪 Learning Objectives

  • Understand core differences between CGI, WSGI, and socket-level web servers.
  • Learn request parsing, routing, and response generation at different abstraction levels.
  • Gain experience with Python’s standard library: http.server, wsgiref, socket, and TinyDB.

🪪 License

This project is licensed under the MIT License.


🧠 Author

Uriah McKinney
Senior Technical Program Manager, Backend Developer-in-Training
GitHubLinkedIn


🗃️ To-Do / Future Work

  • Add logging for request tracing
  • Dockerize the WSGI app
  • Optional: Execute .py scripts dynamically in the socket server

About

This project explores three different ways to serve web content using pure Python—no frameworks. Each server demonstrates a different backend architecture style: CGI Calculator, WSGI Book Catalog, Socket HTTP Server.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published