BYTESTACKDIY
Build Your Own HTTP ServerBuild Your Own HTTP Server
Build / 01PythonStep 1 of 10

Introduction

See what we are building, what we are leaving out, and how to run the project.

An HTTP server can feel like a large piece of infrastructure. Its essential loop is surprisingly small:

  1. listen for a TCP connection;
  2. read bytes that describe a request;
  3. decide what those bytes mean;
  4. send response bytes; and
  5. close or reuse the connection.

In this guide, you will implement that loop with Python's standard library. The finished server will recognize basic HTTP requests, route paths, serve a small file, and return useful errors.

The boundary of our server

We will build a learning-sized HTTP/1.x-style server. It will handle one request per connection and always respond with Connection: close. That keeps connection management visible and lets us focus on the protocol shape.

It's HTTP server without encryption, means we are not going to talk about HTTPS. It will not implement TLS, HTTP/2, request streaming, production-grade limits, or hardened parsing. Those features matter; they are deliberately outside this build.

Only HEAD and GET requests: The server only serves static files as-is.

Simple HTTP server

Let's create a directory first, open your terminal and run the following commands:

Terminal
mkdir bytestack && cd bytestack

Next, create a basic HTML file using the touch command:

Terminal
touch index.html

Open index.html in your preferred text editor and add the following HTML code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Server Message</title>
</head>
<body>
    <h1>Hi from the server</h1>
</body>
</html>

Save the file. Your basic HTML page is now ready to be served.

Now, You need Python 3.11 or newer. So, check your interpreter with:

Terminal
python3 --version

Python has a standard library that we can use to create a server out-of-the-box using http.server. So, to start an HTTP server with it, simply run this command:

Terminal
python3 -m http.server 8000 --bind 127.0.0.1

Congratulations! You have just started an HTTP server with a single command. This will run the server on port 8000, so the local address will be http://127.0.0.1:8000.

Open your browser and search http://localhost:8000/index.html and yes, it will run forever unless you kill this server. Use CTRL+c to interrupt the running server.

Is that it?

No! You just started an HTTP server, but to build your own HTTP server you need to understand:

  • How does it work?
  • How did it start?
  • How is it listening to the request?
  • How is it going to respond?

That was a single command, not code. The actual code is here that runs everytime whenever you run that command.

Now, that we have learned how an HTTP server starts, we are going to build one. Before we build an HTTP server, we are going to learn how your browser (web/client) reaches a server and few more topics on how connections are made.