Introduction to server-side scripting

These days most websites on the Internet have dynamic content. This means that the content displayed to the user has not been written directly into the HTML page but rather it has been generated by selecting information from a database that resides on the server.

This is advantageous as it means that you can create a single template page and then populate it with different information from the database depending on what page is requested.

Common Gateway Interface

The web is a client-server system. A web browser residing on your computer acts as a client which can request web pages from different web server. This can be shown as a diagram:

Model of Client-Server

In order to deliver dynamic content we need to extend the abilities of the web server so that it can do more than merely send static web pages in response to client requests. The common gateway interface (CGI) provides a mechanism to do this.

When serving static web pages the server is normally asked for a file that has a .htm or .html extension e.g. http://mywebsite.com/index.html. If we wish to serve a dynamic page the extension would be different, for example .cgi or .php.

If a request comes in with one of these extensions the web server knows to pass the request to the CGI which then interprets it correctly and executes the script in the particular scripting language.

Once the script has finished executing the CGI then passes the output back to the web server to be delivered as a response to the client request. The diagram demonstrates this:

Model of Client-Server with CGI

Scripting in Python

We will write our server-side scripts in Python as we can reuse most of our knowledge from writing Python programs on the desktop and it is widely used in real world for this purpose. We will also make use of MySQL to store information.