Tutorial on compiling PythonCGI script

Source: Internet
Author: User
Tags python cgi example
This article describes how to compile a PythonCGI script. CGI is an interface connecting Python and server software. if you need it, see if you want to create a webpage using Python, or is it necessary to process data input from a web form? These tasks can be implemented through the Python CGI (public Gateway Interface) script and an Apache web server. When a user requests a specified URL or interacts with a webpage (such as clicking "submit"), the CGI script is enabled by the web server. After the CGI script is called and executed, the output result is used by the web server to create a webpage that is displayed to the user.
Configure the Apache web server to run CGI scripts

In this tutorial, we assume that the Apache web server has been installed and runs. In this tutorial, the Apache web server (version 2.2.15 for CentOS release version 6.5) runs on a local host (127.0.0.1) and listens to port 80. the following Apache commands specify the same:

The code is as follows:

ServerName 127.0.0.1: 80
Listen 80

In the following example, the HTML file is stored in the/var/www/html directory on the web server and specified by the DocumentRoot command (specify the directory where the webpage file is located ):

The code is as follows:

DocumentRoot "/var/www/html"

Request URL: http: // localhost/page1.html

This will return the content of the following file in the web server:

The code is as follows:

/Var/www/html/page1.html

To enable the CGI script, we must specify the location of the CGI script on the web server, and use the ScriptAlias command:

The code is as follows:

ScriptAlias/cgi-bin/"/var/www/cgi-bin /"

The preceding command indicates that the CGI script is saved in the/var/www/cgi-bin directory of the web server. if the request URL contains/cgi-bin/, the CGI script in this directory will be searched.

We must also specify that the CGI script has the execution permission under the/var/www/cgi-bin directory, and specify the CGI script file extension. Run the following command:

 
 
    Options +ExecCGI  AddHandler cgi-script .py 
 

Visit the URL below: http: // localhost/cgi-bin/myscript-1.py

This will call the following script on the web server:

The code is as follows:

/Var/www/cgi-bin/myscript-1.py

Create a CGI script

Before creating a Python CGI script, you need to confirm that you have installed Python (this is usually installed by default, but the installation version may be different ). The script used in this tutorial is written in Python version 2.6.6. You can use any of the following commands (The-V and -- version parameters will display the version number of the installed Python) to check the Python version.

 $ python -V $ python --version

If your Python CGI script is used to process user input data (from a web input form), you will need to import the Python cgi module. This module can process user input data through web input forms. You can import the script using the following statement in your script:

 import cgi

You must also modify the execution permission of the Python CGI script to prevent the web server from being called. You can use the following command to add the execution permission:

 # chmod o+x myscript-1.py

Python CGI example

The two solutions that involve the Python CGI script will be described below:

  • Use a Python script to create a webpage
  • Read and display user input data, and display results on the web page

Note: The Python cgi module is required in solution 2 because it involves user input data from web forms.
Example 1: Use a Python script to create a webpage

For this solution, we will start by creating a webpage containing a single submit button/var/www/html/page1.html.

  Test Page 1  

When the "submit" button is clicked, the/var/www/cgi-bin/myscript-1.py script will be called (specified through the action parameter ). If you specify a "get" request by setting the method parameter to "GET", the server returns the specified webpage. /Var/www/html/page1.html is displayed in the browser as follows:

/Var/www/cgi-bin/the content of the myscript-1.py is as follows:

 #!/usr/bin/python print "Content-Type: text/html" print "" print "" print "CGI Script Output" print "

This page was generated by a Python CGI script.

" print ""

The first statement indicates that this is a python script run using the/usr/bin/Python command. The "Content-Type: text/html" print statement is required to let the web server know the Type of output that accepts the self-CGI script. Other statements are used to output other webpage content in HTML format.

When the "Submit" button is clicked, the following webpage will return:

The main point of this example is that you can decide which information can be returned by CGI scripts. This may include the log file content, the list of current login users, or the date of today. The possibility of having all python libraries during your processing is endless.
Example 2: read and display user input data and display the result on the webpage

For this solution, we will start by creating a webpage/var/www/html/page2.html that contains three input fields and a submit button.

  Test Page 2  

When the "Submit" button is clicked, the/var/www/cgi-bin/myscript-2.py script will be executed (specified through the action parameter ). /Var/www // html/page2.html ):

/Var/www/cgi-bin/the content of the myscript-2.py is as follows:

 #!/usr/bin/python import cgi form = cgi.FieldStorage() print "Content-Type: text/html" print "" print "" print "CGI Script Output" 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 ""

As mentioned above, the import cgi statement is used to ensure that the user can process the data entered through the web input form. Web input forms are encapsulated in a form object called cgi. FieldStorage object. Once the output is started, "Content-Type: text/html" is required because the web server needs to know the output format of the self-CGI script. The data entered by the user can be obtained in the statements containing form ["firstName"]. value, form ["lastName"]. value, and form ["position"]. value. The names in the brackets are the same as those defined in the/var/www/html/page2.html text input field.

When the "Submit" button is clicked on the webpage, the following webpage will be returned.

The main point of this example is that you can easily read and display the data that the user inputs on the web form. In addition to processing data using strings, you can also use Python to convert user input data into numbers used for numerical computation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.