Build Web Server From Scratch With Python.

Deyber Castañeda
The Startup
Published in
4 min readJan 23, 2021

If you are reading an article with this kind of title so maybe your are involved in the world of Web Development and have at least an idea of how web works, if not you can get an overview of it in this article .

The first thing that might come to your head is “Why to create a Web Server from scratch? What inventing the wheel again for?”

A famous thinker used to say: I hear and I forget; I see and I remember; I do and I understand.
I believe to become a better developer you must get a better understanding of the underlying software systems you use on a daily basis and that includes programming languages, compilers and interpreters, databases and operating systems, web servers and web frameworks. And, to get a better and deeper understanding of those systems you must re-build them from scratch, brick by brick, wall by wall.

I hope at this point you’re convinced that it’s a good idea to start re-building different software systems to learn how they work.

But, What is a Web Server then?

It is basically a networking server that sits on a physical server (yeah, a server on a server lol) and waits for a client to send a request. When it receives a request, it generates a response and sends it back to the client. The communication between a client and a server happens using HTTP protocol. A client can be your browser or any other software that speaks HTTP.

Before the client can send a HTTP request though, it first needs to establish a TCP connection with the Web server. Then it sends an HTTP request over the TCP connection to the server and waits for the server to send an HTTP response back.

To establish the TCP connection we will so-called sockets:

import socketsHOST, PORT = '', 8888

listening_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Here we made a socket instance and passed it two parameters. The first parameter is AF_INET and the second one is SOCK_STREAM. AF_INET refers to the address family ipv4. The SOCK_STREAM means connection oriented TCP protocol.

listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)

Then we set the socket option to SOL_SOCKET to manipulate options at the sockets API level. With this done now we can bind the host and port before start to listen from them.

while True:
client_connection, client_address = listen_socket.accept()

The socket must be bound to an address and listening for connections. For that we use the socket.accept() property, The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.

while True:
client_connection, client_address = listen_socket.accept()
request_data = client_connection.recv(1024)
print(request_data.decode('utf-8'))

To receive the data from the socket we use the socket.recv() method, The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by its argument. Decode to utf-8 can help us to avoid problems with the data received.

http_response = b"""\
HTTP/1.1 200 OK

Hello, World!
"""
client_connection.sendall(http_response)
client_connection.close()

Then we set the response and send it using the sendall method and close the connection.

Now let’s try it…

The entire code would be:

import socket

HOST, PORT = '', 8888

listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print('Serving HTTP on port {PORT} ...')
while True:
client_connection, client_address = listen_socket.accept()
request_data = client_connection.recv(1024)
print(request_data.decode('utf-8'))

http_response = b"""\
HTTP/1.1 200 OK

Hello, World, I just created a Web Server...!
"""
client_connection.sendall(http_response)
client_connection.close()

If you execute this program and check in your browser you should get something like this:

You can also simulate a web browser running the Web server fire up a telnet session on the command line specifying a host to connect to localhost and the port to connect to 8888 and then press Enter:

$ telnet localhost 8888
Trying 127.0.0.1 …
Connected to localhost.

At this point you’ve established a TCP connection with the server running on your local host and ready to send and receive HTTP messages. In the picture below you can see a standard procedure a server has to go through to be able to accept new TCP connections.

Conclusion

And that’s the basic model of how a Web server works. To sum it up: The Web server creates a listening socket and starts accepting new connections in a loop. The client initiates a TCP connection and, after successfully establishing it, the client sends an HTTP request to the server and the server responds with an HTTP response that gets displayed to the user. To establish a TCP connection both clients and servers use sockets.

Resources

--

--

Deyber Castañeda
The Startup

Software Developer and Science lover always learning.