1 # Lesson 2 the dynamic web
4 - https://docs.python.org/3
5 - https://docs.python.org/3/library/http.html
6 - https://github.com/python/cpython/blob/3.12/Lib/http/server.py
7 - https://docs.python.org/3/tutorial
8 - https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals
9 - https://developer.mozilla.org/en-US/docs/Glossary/Favicon
13 In the mid to late 1990s the web started becoming dynamic. Web servers might return different content to different users who requested the same URL. A common example of this was a hit counter which would increment each time any user visited the website, or a guest book to allow users to leave messages.
17 The goal is to build a simple dynamic web server.
18 - Understand how the HTTP method and path map to a Python function call.
19 - Build a web server that returns dynamically generated content.
23 ### Build a simple HTTP server in Python
25 Create a file, `ex2.py` and paste the following into it:
30 listen_address = ('localhost', 8000)
31 request_handler = http.server.SimpleHTTPRequestHandler
32 server = http.server.HTTPServer(listen_address, request_handler)
33 server.serve_forever()
35 if __name__ == '__main__':
39 Run it with `python3 ex2.py`. It should work exactly the same as `python3 -mhttp.server` from the previous exercise.
42 ### Return HTML from a function instead of a file
48 listen_address = ('localhost', 8000)
49 request_handler = MyRequestHandler
50 server = http.server.HTTPServer(listen_address, request_handler)
51 server.serve_forever()
53 class MyRequestHandler(http.server.BaseHTTPRequestHandler):
54 def write(self, text):
55 self.wfile.write(text.encode('utf-8'))
58 self.send_response(200)
59 self.send_header('Content-type', 'text/html')
62 self.write('<head><title>My web server!</title></head>')
63 self.write('<body>Hi there!</body>')
66 if __name__ == '__main__':
71 ### Make the HTML dynamic!
73 Add `COUNTER = 42` to the top of the file.
75 Then modify your `GET` handler to return some dynamic HTML! Something like this...
80 self.write(f'We have had <b>{COUNTER}</b> visitors today')
83 You can try adding other information to the response too:
85 self.write('You requested: ' + self.path + '<br>')
86 self.write('You are using this client: ' + self.headers.get('user-agent') + '<br>')
90 ### A few things to note
92 Remember doing raw HTTP requests in the previous exercise? With the code above if a client does `GET /file.txt` then Python's `http.server` library parses the HTTP request and does something like the following:
93 - Creates a new instance of the `MyRequestHandler` class.
94 - Sets the HTTP path as: `self.path = '/file.txt'` for this new instance.
95 - Calls the `do_GET()` function on this new instance, because the HTTP method was `GET`.
97 ### Fix the double-counting bug
99 Notice that if you press `ctrl-shift-R` to reload your counter is going up by two at a time? If you look at the log you can see this is because Firefox is requesting `/favicon.ico`. This is the little icon next to the URL in the address bar. Our site isn't fancy enough for this, so we should modify `do_GET()` to return a `404 not found` for these requests.
103 if not self.path.endswith('.html'):
104 self.send_response(404)
106 self.write('File not found')
110 Now try visiting some URL that doesn't end with `.html` and you'll see your 'not found' message.