In the archive, you will find a simple program (written in Python). Run it:
python server.py
Then open your browser and go to http://127.0.0.1:8080/.
Observe what happens, as well as the messages that appear in the console.
Here is what should appear:
> Request:
GET / HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0
Accept: image/webp,*/*
Accept-Language: fr-FR
Accept-Encoding: gzip, deflate
Connection: keep-alive
> Response:
HTTP/1.1 200 OK
Content-type: text/html
18:39:26
When you change page, for example by going to http://127.0.0.1:8080/test, the same response is returned. Observe the differences in the console.
Then modify the code so that you can navigate between several pages:
/ shows the current time, as before,/test shows a "Hello world!" message,On one of your pages, add the following HTML code:
<form method="get">
Number 1: <input type="text" name="n1" /><br />
Number 2: <input type="text" name="n2" /><br />
<input type="submit" value="Add!" />
</form>
Run the code and observe, in the address bar and in the console, the impact that the numbers you enter have on the request.
Modify the code so that this page displays the sum of the two numbers sent by the form.
Try to store the values passed in GET in a {} dictionary.
Now try to display an image using your web application.
No cheating! Your image must be hosted by your application, not somewhere else. For example, the following code:
<img src="/image.jpg" />
Should display an image of your choice; it is up to you to write the response properly so that the browser understands it.
In Python, you can read files like this:
img = open('file', 'rb')
data = img.read()
img.close()
Change the form method to POST:
<form method="post">
And observe what happens.
Warning: the original code responds as soon as it receives the first
empty line. However, in the case of a POST request, the request contains
data whose length is given by the Content-Length header,
so you will have to change this behavior.
Then modify your code so that it can answer with the sum of the two
numbers using POST instead of GET.
When a resource that does not exist is requested, make your program check whether the file exists, and send it to the client
You can support a few MIME types based on the file extension
Using cookies, make a page that displays a number which is incremented on every request.