What is CGI? Detailed description of Python CGI programming

Source: Internet
Author: User
what is CGI

CGI is currently maintained by NCSA, and NCSA defines CGI as follows:

CGI (Common Gateway Interface), a universal Gateway interface, is a program that runs on a server such as an HTTP server, providing an interface with the client HTML page.

Web browsing

To get a better idea of how CGI works, we can click on a link or URL from the Web page:

1. Use your browser to access the URL and connect to the HTTP Web server.

2. When the Web server receives the request information, it resolves the URL and finds whether the file accessed exists on the server and returns an error message if there is the contents of the returned file.

3. The browser receives the information from the server and displays the received file or error message.

CGI programs can be Python scripts, Perl scripts, shell scripts, C or C + + programs, and so on.

CGI Frame composition

Web server support and configuration

Before you perform CGI programming, make sure that your Web server supports CGI and a CGI-configured handler.

Apache supports CGI configuration:

Set up the CGI directory:

scriptalias/cgi-bin//var/www/cgi-bin/

All HTTP server execution CGI programs are saved in a pre-configured directory. This directory is called the CGI directory and, by convention, it is named the/var/www/cgi-bin directory.

CGI files with the. cgi,python extension can also use the. py extension.

By default, the Linux server configuration runs in the Cgi-bin directory as/var/www.

If you want to specify other directories that run CGI scripts, you can modify the httpd.conf configuration file as follows:

<directory "/var/www/cgi-bin" >   allowoverride None   Options +execcgi   Order allow,deny   allow From all</directory>

Add the. py suffix in AddHandler so that we can access the Python script file at the end of the. PY:

AddHandler cgi-script. cgi. pl. py

The first CGI program

We use Python to create the first CGI program, the file name is hello.py, and the file is in the/var/www/cgi-bin directory with the following contents:

#!/usr/bin/python#-*-coding:utf-8-*-print "content-type:text/html" Print                               # blank line, tell the server to end the head print ' 

After saving the file, modify the hello.py and modify the file permissions to 755:

chmod 755 hello.py

This hello.py script is a simple Python script, the output of the first line of the script "content-type:text/html" is sent to the browser and tells the browser that the content type is "text/html".

Output a blank line with print to tell the server to end the header information.

HTTP Header

The "content-type:text/html" in the contents of the hello.py file is part of the HTTP header, which is sent to the browser to tell the browser the content type of the file.

The HTTP header format is as follows:

HTTP Field Name: Field contents

For example:

Content-type:text/html

The following is a simple CGI script that outputs CGI environment variables:

#!/usr/bin/python#-*-coding:utf-8-*-# filename:test.pyimport osprint "content-type:text/html" Printprint "<meta Charset=\ "utf-8\" > "print" <b> environment variables </b><br> ";p rint" <ul> "for Keys in Os.environ.keys ():    print "<li><span style= ' Color:green ' >%30s </span>:%s </li>"% (Key,os.environ[key]) Print "</ul>"

Get and Post methods

The browser client passes information to the server in two ways, both the GET method and the POST method.

Using the Get method to transfer data

The Get method sends the encoded user information to the server side, and the data information is included on the URL of the request page with "?". The number is divided as follows:

Www.test.com/cgi-bin/hello.py?key1=value1&key2=value2


Some additional comments about the GET request:

Get requests can be cached

GET requests remain in browser history

Get requests can be bookmark-Favorites

GET requests should not be used when handling sensitive data

Get request has a length limit

GET requests should only be used to retrieve data

Simple URL instance: Get method

Here is a simple URL that uses the Get method to send two parameters to a hello_get.py program:

/cgi-bin/test.py?name= Rookie Tutorial &url=www.runoob.com

The following is the code for the hello_get.py file:

#!/usr/bin/python#-*-coding:utf-8-*-# filename:test.py# CGI processing module import CGI, CGITB # creates an instantiated form of fieldstorage = CGI. Fieldstorage () # get Data Site_Name = Form.getvalue (' name ') site_url  = form.getvalue (' url ') print "content-type:text/ HTML "printprint" 

After saving the file, modify the hello_get.py and modify the file permissions to 755:

chmod 755 hello_get.py

Simple Form instance: Get method

The following is an HTML form using the Get method to send two data to the server, the server script submitted is also the hello_get.py file, the hello_get.html code is as follows:

<! DOCTYPE html>

"Recommended"

1. Detailed CGI writing data instance code to text or database

2. Sharing an example tutorial on using CGI to run Python scripts on IIS

3. Creating a simple Web page tutorial instance using the CGI module

4. Share an example tutorial on pythoncgi programming

5. Sample code for XML and modern CGI applications

6. The FastCGI process unexpectedly exits resulting in a 500 error

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.