Introduction to server-side Python

Python CGI Explained

The actual scripting technology you used in the example above, exists of many parts: First there is the scripting language. For the client-side script you used JavaScript, as that is the only script language guaranteed to run in all web browsers. On the server-side, you used Python.

Secondly there is the mechanism to have the server-side script actually getting executed on the server. Here there are many possible solutions, most of which depend on what Operating System and web server software is installed. The gisedu.itc.utwente.nl server we use runs on Windows Server and the Internet Information Server (IIS) software. In the case of Python, that means we can have Python programs (with a .py extension) run as so–called CGI applications. The .py extension tells the Web server that the page contains server script that it should process before delivering the page to the browser. In CGI there is a separation between the coding language (in our case Python) that provides the programming logic, and the CGI shared objects, that are used to communicate with the IIS web server, and gives access to its in– and output mechanisms (regardless of the programming or scripting language used).

Important Note: The CGI module we use here is deprecated since Python version 3.11. This means it will will be removed in version 3.13. On the server we use (gisedu.itc.utwente.nl) we are still running 3.11, so it can be used here. For other installations with a more recent Python, you will have to look for alternative solutions (which we will use to update this exercise soon)...

CGI shared objects

Because our server–side script setup uses the CGI standard, the script has access to a number of objects available on the server. The following describes the most-used of these objects:

Request

Retrieves the values that the browser passes to the server during an HTTP request. Use it to find out what information (parameters) were send by browser input to the server.

Response

Controls what information is sent to a browser in the HTTP response message. Use it to send output to the browser (usually as HTML).

Session

Used to manage and store information about a particular user session.

To be able to use these objects in Python, you use the cgi module, which provides Common Gateway Interface support. This module defines a number of utilities for use by CGI scripts written in Python. The HTTP server places all sorts of information about the request (such as the client hostname, the requested URL, the query string, and lots of other goodies) in the script’s shell environment, executes the script, and sends the script’s output back to the client. The script’s input is connected to the client too, and it is passed via the “QueryString” part of the URL.

The CGI module is intended to take care of the different cases and provide a simpler interface to the Python script. It also provides a number of utilities that help in debugging scripts. Note that these objects are independent of the scripting language, i.e., whether you use JavaScript, VBScript or Python, the CGI objects are named and behave the same.

The Response object

The Response object enables you to control, change and process the information sent to a user by the HTTP response message. In other words, you can build the web page that the user sees by creating a Response object. In CGI Python, the Response object is simply the default script output: In other words, whatever you print to the output, will appear in the HTML page.

The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a header, telling the client what kind of data is following. Python code to generate a minimal header section looks like this:

print ('Content-Type: text/html')
print ('') # this blank line is essential, it signals the end of header

The second section is usually HTML, which allows the client software (the browser) to display nicely formatted text with header, in-line images, etc. Here is Python code that prints a simple piece of HTML:

print ('<h1>CGI script output</h1>')
print ('This is my first <b>CGI</b> script:')
print ('Hello, world....!')

The Request object

The Request object provides access to any information that is passed to the Web server from the browser. Note that a Request made from an HTML form to a server can use either one of the methods below:

The GET method

The parameters are sent in the QUERYSTRING object, attached at the end of the URL as a set of Key-Value pairs. For example, when a user submits the form in figure 4, coded to use the GET method, the parameters that are filled in will appear after the question mark (?) in the URL like this:

http://gisedu.itc.utwente.nl/student/s123456/formreceiver.py?name=my+name&age=my+age&sport=Hockey&Submit=Submit


The POST method

The parameters are sent (unseen to the user) in the hidden FORM object.

Let’s now test this request & response mechanism...