Skip to article frontmatterSkip to article content

And by the way what does the server respond to GET ?

In reality, it’s a bad example because very often the Body of an HTTP response is empty;
but well it’s the general idea to remember;


in the browser

It’s possible to see requests and responses in your browser
via Developer Tools → Network


Let’s make a basic HTTP server

## in your terminal:
## let's go to the course repo
cd /bla-bla-bla/backend

## there's an html folder
cd html

## to launch the server
python -m http.server

## NB: ... 
## at this stage the terminal is blocked
## to kill the server type "Control-C"

then open in your browser http://localhost:8000/index.html (*)

(*) you can also replace localhost with your IP address - we talked about it here

it’s really the simplest possible method to make a server with Python 🐍
but well it’s just a toy you know


A bit less basic

This time we’re going to do it by hand and write some code, still in Python 🐍
it happens in the python/http-servers folder
📢 ⚠️ We look at the file server1_static.py

server1_static.py
"""
kind of cheating as it leverages the standard library
but this is the simplest static server
"""

from http.server import HTTPServer, SimpleHTTPRequestHandler

handler = SimpleHTTPRequestHandler

PORT = 9000
print(f"Open this in your browser:\nhttp://localhost:{PORT}/index.html")

httpd = HTTPServer(('', PORT),  handler)
httpd.serve_forever()

Request processing

The internal operation of an HTTP server is quite simple

  1. Listen on a port (80 by default)

  2. Accept a connection

  3. Read the request

  4. Process the request

  5. Send the response

  6. Close the connection

The important point is the transition between steps 3 and 4 which is the heart of the HTTP server
because it defines how the server will process the request.


Examples made by hand

📢 ⚠️ in the python/http-servers folder, we look at the files:


of course frameworks exist for that !

all this is a bit tedious, that’s why we use frameworks (→ following slides)
but it’s good to understand how it works
still to remember: this story of templates; we’ll talk about it again