So far, we have seen how Python could be used as a scripting language, Object-Oriented Programming language, and GUI Applications factory. Now, we will add to those usages one more: Python as a Web Developing Language. Okay, here we go.
How can Python serve in Web Developing?
Simple websites consist of one or more static HTML pages that have purely informational content. In this type of web pages, information flows in one direction only: from the website to the client. With the progress of software developing as a whole, and web developing in particular, and the increased dependence on Internet as a new market for online payment, and e-purchase, such static one-directional trend of information flow became outdated, and Dynamic web pages has taken the focus. Dynamic web pages are created dynamically using scripts that are called and executed on need. These executable scripts are usually referred to as CGI scripts. CGI stands for Common Gateway Interface. In each web server, a specific directory contains all CGI scripts. CGI scripts can be written using any scripting language: Perl, Shell Scripts, C++, or …, Python.
Writing your First Python CGI Script
Our environment in this article will be an Apache web server on a Red Hat Linux (version 6) box. To minimize the required configuration (and focus on the subject of the article), we will work on the default Apache website, with the default path for CGI executable scripts (/var/www/cgi-bin/)
#!/usr/bin/python print "Content-type:text/html\r\n\r\n" print '<html>' print '<head><title>First CGI Script</title></head>' print '<body>' print '<h1>Hello, I am a Python Web Developer!!</h1>' print '</body>' print '</html>'
3) Save and exit the file:
– Press Esc to move to the command mode.
– Type :wq , then presss Enter.
4) As CGI scripts should be executable, we need to set permissions for the new file to be executable:
chmod a+x /var/www/cgi-bin/firstCGI.py
5) Now, on your client PC, open your web browser and type in the address bar the IP address (or hostname) of the Linux server followed by /cgi-bin/firstCGI.py , then press enter. You should get the following page:
Accepting User Input Using POST Method
As we said in the introduction, CGI was built with two main targets in mind:
First: the capability to get input information from the users (so, the information is no longer flows in one way).
Second: the capability to create dynamic content based on the user’s input.
The most common approach to get input data from the web user is using a method called POST. POST is the preferred method for several reasons. But the most important reasons for preferring POST over other methods are:
– It never sends the input data in the URL.
– The sent parameters are not stored in the browser history.
This makes POST more secure (and hence more preferred) than other methods like GET. In this section we are going to learn how to use the POST method to accept some input data from the user, and dynamically reflect this input in the created web page.
Consider a web page for Students information. The page should display three text boxes, and one dropdown list box to accept four inputs from the student: the full name, the age, the gender, and the bench number. Once filled in, the student should click “Submit” to send the entered data to the CGI script that will process them and create a dynamic content to be displayed back to the student.
To achieve these requirements, follow this procedure:
1) Create/edit the index.html:
2) Type the following html code:
<head><title>Students Information</title></head> <h1>Students Information</h1> <form action="/cgi-bin/post_input.py" method="post"> Name: <input type="text" name="full_name"><br /> Age : <input type="text" name="age" /><br /> Sex : <select name="gender"> <option value="Male">Male</option> <option value="Female">Female</option> </select><br /> B.N : <input type="text" name="bn" /> <br /> <br /> <input type="submit" value="Submit" /> </form>
#!/usr/bin/python from cgi import FieldStorage newform = FieldStorage() full_name = newform.getvalue('full_name') age = newform.getvalue('age') gender = newform.getvalue('gender') bn = newform.getvalue('bn') print "Content-type:text/html\r\n\r\n" print "<html>" print "<head><title>Students Info</title></head>" print "<body>" print "<h1>%s</h1><br />" % (full_name) print "<h2>Age: %s</h2>" % (age) print "<h2>Sex: %s</h2>" % (gender) print "<h2>B.N: %s</h2>" % (bn) print "</body>" print "</html>"
6) Save and exit the file.
7) Change the permissions on the CGI script file to allow execution:
chmod a+x /var/www/cgi-bin/post_input.py
8) Now, open your browser, and just type the IP address (or the hostname) of your Linux server. The default home page should be displayed and look like this:
9) Fill in the form the values for Student Name, Student Age, Gender, and Bench Number, and then click Submit.
10) The following window should load:
11) Let’s repeat it with a female name:
12) Click Submit to get the following Window:
* * * * * *
In this article, we have investigated a new feature of the Python language. We have learned how to use Python as a Web Developing Language. Among many other languages, Python can be used to write CGI scripts for dynamic websites. CGI scripts are executable scripts, usually stored in a specific directory, used to create the dynamic content for web pages.
I hope you find this article useful. See you in the next article.