This article mainly introduced the writing Python CGI script tutorial, the CGI is the Python and the Server Software connection interface, needs the friend to be possible to refer to under
Do you want to use the Python language to create a Web page, or to process data that users enter from a Web form? These tasks can be implemented through a Python CGI (Public Gateway Interface) script and an Apache Web server. A CGI script is enabled by a Web server when a user requests a specified URL or interacts with a Web page, such as clicking the "Submit" button. When the CGI script call completes, its output is used by the Web server to create a Web page that is displayed to the user.
Configure the Apache Web server so that it can run CGI scripts
In this tutorial, we assume that the Apache Web server is already installed and running. This tutorial uses the Apache Web server (version 2.2.15, for CentOS release 6.5) to run on the local host (127.0.0.1) and listens for 80 ports, as the following Apache directive specifies:
The code is as follows:
ServerName 127.0.0.1:80
Listen 80
The HTML files in the example below are stored in the/var/www/html directory on the Web server and specified by the DOCUMENTROOT directive (specify the directory where the Web page files reside):
The code is as follows:
DocumentRoot "/var/www/html"
Now try requesting url:http://localhost/page1.html
This will return the contents 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 need to use the Scriptalias directive:
Copy code code as follows:
scriptalias/cgi-bin/"/var/www/cgi-bin/"
The instructions above indicate that the CGI script is stored in the/var/www/cgi-bin directory of the Web server, and the request URL contains/cgi-bin/that will search the directory for CGI scripts.
We must also make it clear that the CGI script has execute permissions under the/var/www/cgi-bin directory, and also specifies the file name extension of the CGI script. Use the following directive:
?
1 2 3 4 |
<directory "/var/www/cgi-bin" > Options +execcgi AddHandler cgi-script. PY </Directory> |
Visit url:http://localhost/cgi-bin/myscript-1.py Below
This will invoke the script shown below in 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 make sure that you have Python installed (which is usually installed by default, but the installation version may be different). The script used in this tutorial is written using the Python version 2.6.6. You can check the Python version by using either of the following commands (the-V and the--version parameter to display the version number of the installed Python).
?
1 2 |
$ PYTHON-V $ python--version |
If your Python CGI script is to be used to process user input data (from a Web input form), you will need to import the Python CGI module. This module can handle data entered by a user through a Web input form. You can import the script in your script by using the following statement:
?
You must also modify the execution permissions of the Python CGI script to prevent the Web server from being invoked. You can increase execution permissions by using the following command:
?
1 |
# chmod O+x myscript-1.py |
Python CGI Example
The two scenarios involving Python CGI scripts will be described below:
Create a Web page with a Python script
Reads and displays the data entered by the user and displays the results on the Web page
Note: The Python CGI module is required in scenario 2 because it involves users entering data from a Web form.
Example 1: Creating a Web page with a Python script
For this scenario, we'll start by creating a page/var/www/html/page1.html that contains a single submit button.
?
When the "Submit" button is clicked, the/var/www/cgi-bin/myscript-1.py script is called (specified by the action parameter). A "Get" request is specified by setting the method parameter to "get", and the server returns the specified Web page. The/var/www/html/page1.html is displayed in the browser as follows:
The contents of/var/www/cgi-bin/myscript-1.py are as follows:
?
1 2 3 4 5 6 7 |
#!/usr/bin/python print "content-type:text/html" print "print" |
The first line declaration indicates that this is a Python script that runs using the/usr/bin/python command. The "content-type:text/html" print statement is required in order for the Web server to know the output type that is accepted from the CGI script. The remaining statements are used to output the rest of the page content in HTML format.
When the "Submit" button is clicked, the following page will be returned:
The point of this example is that you can decide what information can be returned by a CGI script. This may include the contents of the log file, a list of currently logged in users, or today's date. The chances of owning all the Python libraries when you're dealing with them are endless.
Example 2: Reads and displays the data entered by the user and displays the results on the page
For this scenario, we'll start by creating a page/var/www/html/page2.html with three input fields and a Submit button.
?
When the "Submit" button is clicked, the/var/www/cgi-bin/myscript-2.py script is executed (specified by the action parameter). /var/www//html/page2.html the picture shown in the Web browser is shown below (note that three input fields have been filled in):
The contents of/var/www/cgi-bin/myscript-2.py are 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" |
As mentioned earlier, the import CGI statement is used to ensure that the data entered by the user through the Web input form is handled. The Web input form is encapsulated in a Form object, called a CGI. Fieldstorage object. Once the output is started, "content-type:text/html" is required because the Web server needs to know the output format that is accepted from the CGI script. The data entered by the user is available in statements that contain form["FirstName"].value,form["LastName"].value, and form["position"].value. The names in brackets are consistent with the name parameters defined in the/var/www/html/page2.html text entry field.
When the "Submit" button on the page is clicked, the following page will be returned.
The point of this example is that you can easily read and display the data that the user entered on the Web form. In addition to handling data as strings, you can also use Python to convert data entered by the user into numbers that can be used for numeric calculations.