Learn how to use Python as a Web Developing Language

0
4315
Python Tutorial – Advanced Topics_11

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/)

To start writing your first CGI script using Python, follow this procedure:

1) Start the text editor (preferably vim).


2) Type the following Python code:

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:

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.
Ultimate Java Development and Certification Course

To achieve these requirements, follow this procedure:

1) Create/edit the index.html:
3

2) Type the following html code:

3) Save and exit the file.
4) Create the file /cgi-bin/post_input.py


5) Type the following Python Code (CGI Script):

6) Save and exit the file.
7) Change the permissions on the CGI script file to allow execution:

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.
6

10) The following window should load:
7

11) Let’s repeat it with a female name:
8

12) Click Submit to get the following Window:
9

Good Work!!

* * * * * *

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here