Tutorial on compiling a Python CGI script

Source: Internet
Author: User
Tags python cgi example

Tutorial on compiling a Python CGI script

This article describes how to compile a Python CGI script. CGI is an interface connecting Python to server software. For more information, see

Do you want to use Python to create a web page or 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:

Copy the Code 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:

?

1

2

3

4

<Directory "/var/www/cgi-bin">

Options + ExecCGI

AddHandler cgi-script. py

</Directory>

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.

?

1

2

$ 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:

?

1

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:

?

1

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

?

1

2

3

4

5

6

<Html>

<H1> Test Page 1

<Form name = "input" action = "/cgi-bin/myscript-1.py" method = "get">

<Input type = "submit" value = "Submit">

</Form>

</Html>

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:

?

1

2

3

4

5

6

7

#! /Usr/bin/python

Print "Content-Type: text/html"

Print ""

Print "

Print "

Print "<p> This page was generated by a Python CGI script. </p>"

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.

?

1

2

3

4

5

6

7

8

9

<Html>

<H1> Test Page 2

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

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:

?

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 "<p>"

Print "The user entered data are: <br>"

Print "<B> First Name: </B>" + form ["firstName"]. value + "<br>"

Print "<B> Last Name: </B>" + form ["lastName"]. value + "<br>"

Print "<B> Position: </B>" + form ["position"]. value + "<br>"

Print "</p>"

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.