Build a Simple Python Web Server from Scratch – Step‑by‑Step Guide

This article walks you through the philosophy of rebuilding software systems, explains what a network server is, and provides a detailed, hands‑on tutorial for creating a minimal Python web server, covering URLs, sockets, HTTP requests, and responses.

ITPUB
ITPUB
ITPUB
Build a Simple Python Web Server from Scratch – Step‑by‑Step Guide

Motivation

Re‑implementing a network server from scratch helps developers understand how the components of a modern software stack—languages, compilers, databases, operating systems, network protocols, and web frameworks—fit together.

What Is a Network Server?

A network server is a program that runs on a physical machine, creates a listening socket, accepts incoming TCP connections, parses HTTP requests, and sends HTTP responses. Clients can be browsers or any HTTP‑capable software.

Minimal Python Server

The following Python script implements a very small HTTP server that listens on port 8888 and returns Hello, World! for the path /hello. Save it as webserver1.py and run it from the command line.

import socket

HOST = ''          # Listen on all interfaces
PORT = 8888

# Create a TCP socket and bind it to the address
listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_sock.bind((HOST, PORT))
listen_sock.listen(5)
print('Listening on port', PORT)

while True:
    client_sock, client_addr = listen_sock.accept()
    request = client_sock.recv(1024).decode('utf-8')
    # Simple handling: always respond with the same message
    response = ('HTTP/1.1 200 OK
'
                'Content-Type: text/plain; charset=utf-8
'
                '
'
                'Hello, World!')
    client_sock.sendall(response.encode('utf-8'))
    client_sock.close()

Run the server: python webserver1.py Then open a browser and navigate to http://localhost:8888/hello. The page should display Hello, World!

Understanding URLs and TCP Connections

A URL (Uniform Resource Locator) tells a client which host and which resource to request. Before an HTTP request is sent, the client establishes a TCP connection to the server’s IP address and port using a socket.

Manual Request with Telnet

You can simulate a browser with the telnet command: telnet localhost 8888 After the connection is open, type the request line and press Enter twice: GET /hello HTTP/1.1 The server will reply with the same HTTP response shown above.

HTTP Request and Response Structure

An HTTP request line consists of three parts:

Method (e.g., GET)

Path (e.g., /hello)

Protocol version (e.g., HTTP/1.1)

The corresponding response starts with a status line, followed by a blank line and the body:

HTTP/1.1 200 OK

Hello, World!

In this minimal server the request is not parsed; any data sent by the client results in the same static response.

Summary of Server Operation

Create a listening socket on the desired port.

Enter an infinite loop that accept() s new connections.

Read the incoming bytes (the HTTP request).

Send a fixed HTTP response containing a status line, required headers, a blank line, and the response body.

Close the client socket and wait for the next connection.

This implementation can be tested with a web browser, curl, or telnet. It demonstrates the core workflow of a web server without any external dependencies.

Challenge

Without modifying webserver1.py, devise a way to run applications built with Django, Flask, and Pyramid on the same server, satisfying each framework’s requirements. The solution is explored in the next part of the series.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPythonHTTPNetwork programming
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.