The above two cases can be addressed by using CGI programming using Python. In this session we will discuss CGI programming using Python.
The Common Gateway Interface (CGI) is the standard method to communicate with programs or script using html interface. There are three parts in this - first is an html page which will pass the data from the client side. Second is web server software, that delivers pages to client on the request using the Hypertext Transfer Protocol and third one is programming language, like C, Perl, PHP Java, or Visual Basic, python using which, scripts can be written for specific purposes. The prominence of CGI program is, user can write programs in the favorite platform and it can be used for webpage development, which will interact with the user dynamically.
Unlike other programming languages, Python CGI programming is very easy. To try a CGI program you need to install web server which helps to access files through http through Internet or in the same computer which we are using. Here we can use Apache server, which was very instrumental in the revolution of World Wide Web and is commonly used. Apache server supports most of the languages like PHP, Perl, and Python.
You can download Apache server from http://apache.org as per your operating system. Now install Apache server in your PC. To check whether the installation of Apache server is successful, open the browser and type local host. If the browser displays “It works!” then you are ready for action.
Almost all Linux platforms supports CGI programming and has pre configured directory called CGI directory usually named as cgi bin. For Linux users, this directory can be found at /var/www/cgi-bin.
Now the first step is to make the web server to execute the python scripts. This can be done by adding .py in the AddHandler block (line 393) of httpd.conf file (this can be found at the path /var/www in linux). Add .py preceding to the line AddHandler cgi-script .cgi
Windows users can locate conf folder in the directory where you installed Apache. Inside conf folder you can find httpd.conf file.
That is, finally the AddHandler line will be AddHandler cgi-script .cgi .py
Yes!!! Now your webserver is equipped to do CGI programming using Python. In the server side, to show the path of the python executable installed, specify the path of the python.exe in the first line of the python file. For this, code is as follows:
#!C:/Python27/python.exe (for Windows- If Python is installed in C:/Python27)
#!/usr/bin/python (for Linux)
Now write the following python program and save it in cgi-bin directory.
#!D:/Python27/python.exe print "Content-type:text/html\r\n\r\n" print '<html>' print '<head>' print '<h1>I begin my CGI journey in Python</h1>' print '</head>' print '</html>'
In browser, type - http://localhost/cgi-bin/example.py. This will give you the following output:
Here we have received output in browser by running a python script. Or in other words, we can say that we have printed html page using python language.
In some cases we need to process or save data entered through html form as for registration forms and verification of email address. Let us make an html page through which you can enter your name and python scripts to process the name and display “hello ‘your name’ ”. For this we need to create an html page by specifying the root and name of the python file located at cgi-bin.
Html code
<html> <body> <form method="POST" action="http://localhost/cgi-bin/form.py"> <p>Please Enter Your Name: <input type="text" name="firstname"> <p>Click here to submit form: <input type="submit"> </form> </body> </html>
form.html
Here we have made an html page which consists of one text box and a submit button. Here the name entered in the text box is stored in the variable firstname. When you submit the value this will be passed to the python file - form.py located in cgi-bin. Now we need to write Python program to receive the variable sent from the html page, and to process and print into another html page.
Python code
#!D:\Python27\python import cgi def main(): print "Content-type: text/html\n" form = cgi.FieldStorage() # parse query print "<h1>Hello", form["firstname"].value, "</h1>" main()
form.py
In the program, first line describes the path of the python executable. The CGI program starts by importing cgi module. Content-type describes the format of the file being returned by the python program. Here we have used Content-type as html (Content-type: text/html).
Now to parse the query, the total content is transferred to the variable - form. Each value stored in the variable form is obtained by using the code form["The name of input initiated in the html"].value
Methods of sending browser information to the web server are GET and POST. In GET method, data is encoded into a URL, so that we can see the variables passing to the program. In POST method, data is encoded as messages inside the body. POST method can be used for sending data of large size, or sending passwords etc.