Windows Server Operations Series

🛠️ A "Top 1%" Hands-On Lab: Writing Your Own HTTP Server From Scratch

Introduction

This article is part of the Top 1% Series: Full Article Guide, the 7th in the Windows Server Operations Series. If you already have the Linux environment from the Hands-On Prep Manual (or any machine with Python 3), you can do this with no additional software installation.

Prerequisite Knowledge

Getting the Big Picture

Everything you’ll build in this hands-on lab is just these 3 steps.

graph LR
    Step1["Step 1<br/>Listen for connections<br/>on a TCP socket"]
    Step2["Step 2<br/>Read the incoming<br/>bytes as text"]
    Step3["Step 3<br/>Write back a string<br/>in a fixed format"]
    Step1 --> Step2 --> Step3

What IIS, Nginx, and Apache do is, at its core, an extension of these same 3 steps. The difference is that these production implementations pile on massive concurrent-connection handling, careful conformance to HTTP’s many edge cases, security hardening, and performance optimization.

Hands-On Steps

Step 1: Write a minimal HTTP server

Create a file called mini_server.py in any directory and write the following code. The key point is that it uses nothing but a raw TCP socket — not even Python’s built-in http.server module, let alone a web framework.

import socket

HOST = "0.0.0.0"
PORT = 8080

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(5)
print(f"Listening on {HOST}:{PORT} ...")

while True:
    conn, addr = server_socket.accept()
    request_bytes = conn.recv(4096)
    request_text = request_bytes.decode("utf-8", errors="replace")

    # Pull out just the first line (the request line)
    request_line = request_text.split("\r\n")[0]
    print(f"[{addr}] {request_line}")

    method, path, _ = request_line.split(" ")

    if path == "/hello":
        body = "<h1>Hello from my own HTTP server!</h1>"
    else:
        body = "<h1>It works.</h1><p>This page is served by code you wrote yourself.</p>"

    response = (
        "HTTP/1.1 200 OK\r\n"
        "Content-Type: text/html; charset=utf-8\r\n"
        f"Content-Length: {len(body.encode('utf-8'))}\r\n"
        "Connection: close\r\n"
        "\r\n"
        f"{body}"
    )
    conn.sendall(response.encode("utf-8"))
    conn.close()

Run it with python3 mini_server.py, and your terminal will show Listening on 0.0.0.0:8080 ....

Step 2: Access it from a real browser

From a browser, open http://<the machine's IP address>:8080/, pointing at the IP address of the machine running the server. You’ll see “It works.” displayed as a page returned by the Python code you wrote yourself. Next, visit http://<IP address>:8080/hello, and you’ll see a different HTML response depending on the value of path.

Those few lines — if path == "/hello": — are, at their core, the most primitive version of what frameworks like ASP.NET or Express call “routing.” A framework just wraps this same branching logic into something convenient — URL pattern matching, configuration files — but the underlying substance of what’s happening doesn’t change.

Step 3: Look at the actual bytes flowing over the wire

The -v flag on curl lets you see the raw HTTP messages being sent and received.

curl -v http://<IP address>:8080/

In the output, lines starting with > are the request you sent, and lines starting with < are the response you received. You can confirm that the HTTP/1.1 200 OK and Content-Type lines you assembled in your server code arrive at the client exactly as those literal strings. Using the packet-capture tool covered in How to Use Wireshark, you can also watch these same strings flow by, unchanged, as the TCP payload.

The View From the Top 1% Perspective

What this mini server is missing — why production actually uses IIS or Nginx

These few dozen lines of code are enough to viscerally experience the idea that “any software that can process HTTP can be a web server,” but they’re missing the following elements needed for production use:

“I could build this myself” and “in production, you should use a proven implementation instead of building your own” are not remotely in conflict. In fact, being able to make the judgment that “some things are best left to the specialists” — while genuinely understanding the underlying principle — is exactly what top-1% understanding looks like.

Common Misconceptions and Pitfalls

The Troubleshooting Perspective

  1. Can’t reach it from a browser: Check whether the firewall on the machine running the server allows inbound traffic on port 8080.
  2. Getting an Address already in use error: Check whether another process is already listening on the same port (e.g. a previous run of the server that hasn’t fully exited). Even with socket.SO_REUSEADDR set, some operating systems won’t let you reuse a port immediately.

Summary

What to Keep in Mind From Today

  1. When you see the word “website,” remember that its true nature is “software that can process HTTP.”
  2. When you encounter a convenient framework feature, get in the habit of asking “what simple mechanism is this, in principle, an extension of?”

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).