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
"""
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
Listen on a port (80 by default)
Accept a connection
Read the request
Process the request
Send the response
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:
server2_static_byhand.pybasically, same functions: knows how to respond to GET for static files
but written “by hand”
server3_post_stateful.pythe server is STATEFUL (it remembers the state) - see the
STATEvariable
(NB: in real life of course, the state will be stored in a SQL database - or other)the POST: var=value assignments are memorized
the GET: whatever the PATH, displays in html the content of variables known in
STATE(and other details)
server4_template.pysame functionalities but with a JINJA2 template
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