In the previous video, I introduced a lot of new concepts that may or
may not be familiar to you.
To see all of these concepts in action,
in this section we're going to build our own web server in Python that not only
renders web pages in a browser, but by processing data submitted by the user,
can create a unique web page each time a user modifies the input.
To get started let's create a new Python file called webserver.py and
save it in the same directory as your restaurantmenu.db.
And databasesetup.py files so we can access it from our vagrant machine.
Follow along with me using Sublime or your favorite text editor.
One of the cool things about Python is that it comes with a large collection of
libraries we can use right out of the box.
We will be using the HTTP base server library extensively to
create this project.
Take a minute to skim over the HTTP base server documentation on
the official Python documentation website.
I will add from BaseHTTPServer import BaseHTTPRequestHandler and
HTTPServer to the first line of my webserver.py file to be able to
take advantage of this BaseHTTPServer functionality.
Our web server code will have two main sections, the main method and
the handler class.
In main we instantiate our server and specify what port it will listen on.
Our handler code indicates what code to execute based on the type of
HTTP request that is sent to the server.
I will start by coding a main method as the entry point of my code.
I'll add this block of code at the end of my file to immediately run
the main method when the Python interpreter executes my script.
Inside this main method, I will add a try except block.
The Python interpreter will try to attempt the code inside the try block.
And if a defined event occurs, we can exit out of the code with an exception.
The exception I want to handle is KeyboardInterrupt.
This is a built-in exception in Python that can be triggered when
the user holds Ctrl+C on the keyboard.
Going back inside my try block,
I will create an instance of an HTTP server class.
Looking at the documentation for
HTTPServer, I see that I can create a web server like so.
In the documentation we also see that HTTPServer is built off of a TCPServer
class, indicating the transmission protocol that this server will use.
Server address is a tuple that contains the host and port number for our server.
I will leave host as an empty string and specify our port number with an integer.
I will define port in a separate variable, and
create my server instance as so.
Notice that I just made up a name, webserverHandler, for
the request handler class.
I will define my webserverHandler in the next section of code.
But first, let me add some finishing code to the main method.
After I create the server I'll add a print statement to know that my
server is running.
Then I will use this serve_forever function built into my HTTPServer to
keep it constantly listening until I call Ctrl+C or exit the application.
Inside my KeyboardInterrupt exception I will shut down the server by
calling server.socket.close.
And I have another print statement,
just letting me know that the server was properly stopped.
Above my main method,
I will now define the webserverHandler class that I called in my HTTPServer,
and have it extend from a class called BaseHTTPRequestHandler.
The do_GET function handles all get requests our web server receives.
In order to figure out which resource we are trying to access, we will use
a simple pattern matching plan that only looks for the ending of our URL path.
Let's create another try except block for our do_GET method.
The BaseHTTPRequestHandler provides us a variable called path
that contains the URL sent by the client to the server as a string.
I will make an if statement that looks for the URL that ends with /hello.
I will tell my web server to send a response code of
200 indicating a successful get request.
I will use the send_header function to indicate that I'm replying with text in
the form of HTML to my client along with this end_headers command which just
sends a blank line indicating the end of our HTTP headers in the response.
Now that I've created the response I can include some content to
send back to the client.
I'll create an empty string for now and name it output.
In the next line I will add a message with some HTML and
body tags to my output stream.
I can use the self.wfile.write function to send a message back to the client.
So I'll go ahead and put my output string there.
I'm going to add a print statement for
now, just so I can see my output string in the terminal.
This might come in handy for debugging.
Then I can exit my if statement with the return command.
I will also write an exception for IOErrors, and
use the following line of code to notify me of a 404 or file not found error.
Now, I think I'm ready to test and
see what I've written thus far in the browser.