Simple HTTP Server With Python
Bode Okunfolami • June 7, 2024
pythonA web server is software that processes, stores, and transfers data over the internet. The data transferred by a web server is just text that moves between the browser and the server. The text is then read by a browser to display the website. Today, web servers are built using web frameworks with pre-configured web servers for development.
This post shows how to build a web server from scratch using Python 3 and explains the basic concepts of web servers and how they work. The only thing you need is a basic understanding of Python 3.
HTTP messages
HTTP (Hypertext Transfer Protocol) is a method of communication between browsers and web servers. HTTP is also used to send and retrieve data from a web server. The data exchanged between a server and a browser is called an HTTP message. There are two types of messages; requests sent by the browser to the server and responses, the answer from the server. Here is how to use a request message to retrieve the home page of a website:
GET /home.html HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0
When we enter the URL http://localhost/home.html
on a browser the above request message is sent from the browser to the server.
Processing HTTP requests using sockets
To build a web server from scratch we will use network sockets. A socket is an interface that runs on a network, through which an application can send and receive data. Here is an implementation of an HTTP server using socket:
import socket
HOST, PORT = '0.0.0.0', 8080 # Define socket host and port
# Create socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Reuse socket in TIME_WAIT state
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Attach host and port to socket
sock.bind((HOST, PORT))
# Queue up to 5 connections
sock.listen(5)
while True: # Run forever
connection, address = sock.accept() # Accept incoming connection
# HTTP request message
request = connection.recv(1024)
print(request.decode())
# HTTP response message
response = '''HTTP/1.1 200 OK
Hello World'''
# Send response back to client and close connection
connection.sendall(response.encode())
connection.close()
We created a socket object with Python's socket library. The socket.AF_INET
represents the IPv4 address family. The IPv4 address family is used for identifying devices on the network. The socket.SOCK_STREAM
specifies a TCP (Transport Control Protocol) connection that ensures data integrity and delivery.
The HOST
is the address of the device on the network while the PORT
is the location where the socket is running on the device. The socket object's bind
method associates the socket with an address and a port number. This is how a client locates and connects to the socket.
The while loop will keep the server running even after a connection is closed. The socket object's accept
method connects a client with the server and returns a tuple of the client's connection and address. Data from the client is then read by the server using the connection's recv
method, which specifies the maximum number of bytes to be received. Finally, we send a response message back to the client using the connection's sendall
method. Note that the data received from the client is in bytes and needs to be converted to a string using the decode
method. Similarly, when sending data to the client, it must be in bytes, so we use the encode
method to convert the response string to bytes.
When you navigate to the URL http://localhost:8080
on your browser you will see a server response.
The image above shows the response from the server while the request message sent to the server using the Mozilla Firefox browser is below:
GET / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br, zstd
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Priority: u=1
At its core, a web server operates as a network socket facilitating data transfers between the server and client. HTTP enables browsers to communicate with the server. TCP, a type of network socket, ensures that HTTP messages (data) are transferred reliably and in the correct order.