http://xmodulo.com/create-use-python-cgi-scripts.html
Have you ever wanted to create a webpage or process user input from a web-based form using Python? These tasks can be accomplished through the use of Python CGI (Common Gateway Interface) scripts with an Apache web server. CGI scripts are called by a web server when a user requests a particular URL or interacts with the webpage (such as clicking a "Submit" button). After the CGI script is called and finishes executing, the output is used by the web server to create a webpage displayed to the user.
HTML files used in the upcoming examples are located in /var/www/html on the web server. This is specified via the DocumentRoot directive (specifies the directory that webpages are located in):
Consider a request for the URL: http://localhost/page1.html
This will return the contents of the following file on the web server:
The above directive indicates that CGI scripts are contained in the /var/www/cgi-bin directory on the web server and that inclusion of /cgi-bin/ in the requested URL will search this directory for the CGI script of interest.
We must also explicitly permit the execution of CGI scripts in the /var/www/cgi-bin directory and specify the file extensions of CGI scripts. To do this, we use the following directives:
Consider a request for the URL: http://localhost/cgi-bin/myscript-1.pyHave you ever wanted to create a webpage or process user input from a web-based form using Python? These tasks can be accomplished through the use of Python CGI (Common Gateway Interface) scripts with an Apache web server. CGI scripts are called by a web server when a user requests a particular URL or interacts with the webpage (such as clicking a "Submit" button). After the CGI script is called and finishes executing, the output is used by the web server to create a webpage displayed to the user.
Configuring the Apache web server to run CGI scripts
In this tutorial we assume that an Apache web server is already set up and running. This tutorial uses an Apache web server (version 2.2.15 on CentOS release 6.5) that is hosted at the localhost (127.0.0.1) and is listening on port 80, as specified by the following Apache directives:
1
2
| ServerName 127.0.0.1:80 Listen 80 |
1
| DocumentRoot "/var/www/html" |
This will return the contents of the following file on the web server:
/var/www/html/page1.htmlTo enable use of CGI scripts, we must specify where CGI scripts are located on the web server. To do this, we use the ScriptAlias directive:
1
| ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" |
We must also explicitly permit the execution of CGI scripts in the /var/www/cgi-bin directory and specify the file extensions of CGI scripts. To do this, we use the following directives:
1
2
3
4
|
Options +ExecCGI AddHandler cgi-script .py
|
This will call the following script on the web server:
/var/www/cgi-bin/myscript-1.py
Creating a CGI script
Before creating a Python CGI script, you will need to confirm that you have Python installed (this is generally installed by default, however the installed version may vary). Scripts in this tutorial are created using Python version 2.6.6. You can check your version of Python from the command line by entering either of the following commands (the -V and --version options display the version of Python that is installed):
$ python -V
$ python --version
If your Python CGI script will be used to process user-entered data
(from a web-based input form), then you will need to import the Python cgi
module. This module provides functionality for accessing data that
users have entered into web-based input forms. You can import this
module via the following statement in your script:$ python --version
1
| import cgi |
# chmod o+x myscript-1.py
Python CGI Examples
Two scenarios involving Python CGI scripts will be considered in this tutorial:- Create a webpage using a Python script
- Read and display user-entered data and display results in a webpage
Example 1: Create a webpage using a Python script
For this scenario, we will start by creating a webpage /var/www/html/page1.html with a single submit button:
1
2
3
4
5
6
| < html > < h1 >Test Page 1</ h1 > < form name = "input" action = "/cgi-bin/myscript-1.py" method = "get" > < input type = "submit" value = "Submit" > </ form > </ html > |
The contents of /var/www/cgi-bin/myscript-1.py are:
1
2
3
4
5
6
7
| #!/usr/bin/python print "Content-Type: text/html" print "" print "" print " print "This page was generated by a Python CGI script. print " |
When the "Submit" button is clicked in the above webpage, the following webpage is returned:
The take-home point with this example is that you have the freedom to decide what information is returned by the CGI script. This could include the contents of log files, a list of users currently logged on, or today's date. The possibilities are endless given that you have the entire Python library at your disposal.
Example 2: Read and display user-entered data and display results in a webpage
For this scenario, we will start by creating a webpage /var/www/html/page2.html with three input fields and a submit button:
1
2
3
4
5
6
7
8
9
| < html > < h1 >Test Page 2</ h1 > < form name = "input" action = "/cgi-bin/myscript-2.py" method = "get" > First Name: < input type = "text" name = "firstName" >< br > Last Name: < input type = "text" name = "lastName" >< br > Position: < input type = "text" name = "position" >< br > < input type = "submit" value = "Submit" > </ form > </ html > |
The contents of /var/www/cgi-bin/myscript-2.py are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| #!/usr/bin/python import cgi form = cgi.FieldStorage() print "Content-Type: text/html" print "" print "" print " print "" print "The user entered data are: print "First Name: " + form[ "firstName" ].value + " print "Last Name: " + form[ "lastName" ].value + " print "Position: " + form[ "position" ].value + " print " |
print
"
When the "Submit" button is clicked in the above webpage, the following webpage is returned:
The take-home point with this example is that you can easily read and display user-entered data from web-based input forms. In addition to processing data as strings, you can also use Python to convert user-entered data to numbers that can be used in numerical calculations.
No comments:
Post a Comment