+<h1>Lesson 2 the dynamic web</h1>
+<h2>References:</h2>
+<ul>
+<li>https://docs.python.org/3</li>
+<li>https://docs.python.org/3/library/http.html</li>
+<li>https://github.com/python/cpython/blob/3.12/Lib/http/server.py</li>
+<li>https://docs.python.org/3/tutorial</li>
+<li>https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals</li>
+<li>https://developer.mozilla.org/en-US/docs/Glossary/Favicon</li>
+</ul>
+<h2>Overview</h2>
+<p>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.</p>
+<h2>Learning objective</h2>
+<p>The goal is to build a simple dynamic web server.<br />
+- Understand how the HTTP method and path map to a Python function call.<br />
+- Build a web server that returns dynamically generated content.</p>
+<h2>Exercises</h2>
+<h3>Build a simple HTTP server in Python</h3>
+<p>Create a file, <code>ex2.py</code> and paste the following into it:</p>
+<pre><code>import http.server
+
+def main():
+ listen_address = ('localhost', 8000)
+ request_handler = http.server.SimpleHTTPRequestHandler
+ server = http.server.HTTPServer(listen_address, request_handler)
+ server.serve_forever()
+
+if __name__ == '__main__':
+ main()
+</code></pre>
+<p>Run it with <code>python3 ex2.py</code>. It should work exactly the same as <code>python3 -mhttp.server</code> from the previous exercise.</p>
+<h3>Return HTML from a function instead of a file</h3>
+<pre><code>import http.server
+
+def main():
+ listen_address = ('localhost', 8000)
+ request_handler = MyRequestHandler
+ server = http.server.HTTPServer(listen_address, request_handler)
+ server.serve_forever()
+
+class MyRequestHandler(http.server.BaseHTTPRequestHandler):
+ def write(self, text):
+ self.wfile.write(text.encode('utf-8'))
+
+ def do_GET(self):
+ self.send_response(200)
+ self.send_header('Content-type', 'text/html')
+ self.end_headers()
+ self.write('<html>')
+ self.write('<head><title>My web server!</title></head>')
+ self.write('<body>Hi there!</body>')
+ self.write('</html>')
+
+if __name__ == '__main__':
+ main()
+</code></pre>
+<h3>Make the HTML dynamic!</h3>
+<p>Add <code>COUNTER = 42</code> to the top of the file.</p>
+<p>Then modify your <code>GET</code> handler to return some dynamic HTML! Something like this...</p>
+<pre><code>global COUNTER
+COUNTER = COUNTER + 1
+self.write(f'We have had <b>{COUNTER}</b> visitors today')
+</code></pre>
+<p>You can try adding other information to the response too:</p>
+<pre><code>self.write('You requested: ' + self.path + '<br>')
+self.write('You are using this client: ' + self.headers.get('user-agent') + '<br>')
+</code></pre>
+<h3>A few things to note</h3>
+<p>Remember doing raw HTTP requests in the previous exercise? With the code above if a client does <code>GET /file.txt</code> then Python's <code>http.server</code> library parses the HTTP request and does something like the following:<br />
+- Creates a new instance of the <code>MyRequestHandler</code> class.<br />
+- Sets the HTTP path as: <code>self.path = '/file.txt'</code> for this new instance.<br />
+- Calls the <code>do_GET()</code> function on this new instance, because the HTTP method was <code>GET</code>.</p>
+<h3>Fix the double-counting bug</h3>
+<p>Notice that if you press <code>ctrl-shift-R</code> 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 <code>/favicon.ico</code>. 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 <code>do_GET()</code> to return a <code>404 not found</code> for these requests.</p>
+<p>Something like this:</p>
+<pre><code>if not self.path.endswith('.html'):
+ self.send_response(404)
+ self.end_headers()
+ self.write('File not found')
+ return
+</code></pre>
+<p>Now try visiting some URL that doesn't end with <code>.html</code> and you'll see your 'not found' message.</p>
\ No newline at end of file