Python 2 CGI Programming
What is CGI
CGI is currently maintained by NCSA, and NCSA defines CGI as follows:
CGI (Common Gateway Interface), a common gateway interface, which is a program that runs on a server such as an HTTP server and provides an interface to the client's HTML page.
To better understand 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. After receiving the request information, the web server will parse the URL and find out whether the accessed file exists on the server. If there is content of the returned file, an error message is returned.
- 3. The browser receives 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 more.
CGI architecture diagram
Web Server Support and Configuration
Before you do CGI programming, make sure your web server supports CGI and CGI handlers.
Apache supports CGI configuration:
Set up the CGI directory:
ScriptAlias /cgi-bin/ var/www/cgi-bin/
All HTTP server execution CGI programs are stored 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 have a .cgi extension and python 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 a different directory to 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 a .py suffix to the AddHandler so that we can access the python script file ending in .py:
AddHandler cgi- Span>script .cgi . pl .py
The first CGI program
We created the first CGI program in Python with the file name hello.py and the file in the /var/www/cgi-bin directory with the following content:
#!/usr/bin/python # -*- coding: UTF-8 -*- print "Content-type:text/html" print # blank line, telling the server to end the header print '<html>' print '<head>' print '<meta charset="utf-8"> ' print '<title>Hello World - my first CGI program! </title>' print '</head>' print '<body>' print '<h2>Hello World! I am from the welookups tutorial First CGI program </h2>' print '</body>' print '</html>' Pre>
Modify hello.py after saving the file, and modify the file permissions to 755:chmod 755 hello Span>.py
The above program is accessed in the browser http://localhost/cgi-bin/hello.py and the results are as follows:
Hello World! I am the first from the welookups tutorial CGIprogram
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 to display the content type "text/html". .
Use print to output a blank line to tell the server to end the 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 format of the HTTP header is as follows:HTTP Field Name: /span>Field Content
For example:Content-type: text/html
The following table describes the information commonly used by HTTP headers in CGI programs:
|Content-type:||The requested MIME information corresponding to the entity. For example: Content-type:text/html|
|Expires: Date||Time and time when the response expired|
|Location: URL||is used to redirect the recipient to the location of the unsolicited URL to complete the request or identify the new resource|
|Last-modified: Date||Reserved resource last modified time|
|Content-length: N||Requested content length|
|Set-Cookie: String||Set Http Cookie|
CGI environment variable
All CGI programs receive the following environment variables, which play an important role in CGI programs:
|CONTENT_TYPE||The value of this environment variable indicates the MIME type of the information passed. Currently, the environment variable CONTENT_TYPE is generally: application/x-www-form-urlencoded, which means that the data comes from an HTML form.|
|CONTENT_LENGTH||If the server and CGI program information is passed in POST, this environment variable can read the number of bytes of valid data even from the standard input STDIN. This environment variable must be used when reading the entered data.|
|HTTP_COOKIE||COOKIE content within the client.|
|HTTP_USER_AGENT||Provides client browser information that includes the number of versions or other proprietary data.|
|PATH_INFO||The value of this environment variable represents the other path information immediately after the CGI program name. It often appears as a parameter to CGI programs.|
|QUERY_STRING||If the server and CGI program information is passed in GET, the value of this environment variable is even if the information is passed. This information follows the name of the CGI program, separated by a question mark '?'.|
|REMOTE_ADDR||The value of this environment variable is the IP address of the client that sent the request, such as 192.168.1.67 above. This value always exists. And it is the unique identifier that the Web client needs to provide to the Web server, which can be used in CGI programs to distinguish between different Web clients.|
|REMOTE_HOST||The value of this environment variable contains the hostname of the client that sent the CGI request. If you do not support the query you want, you do not need to define this environment variable.|
|REQUEST_METHOD||Provides the method by which the script is called. For scripts that use the HTTP/1.0 protocol, only GET and POST make sense.|
|SCRIPT_FILENAME||Full path to CGI script|
|SCRIPT_NAME||The name of the CGI script|
|SERVER_NAME||This is the host name, alias or IP address of your web server.|
|SERVER_SOFTWARE||The value of this environment variable contains the name and version number of the HTTP server that called the CGI program. For example, the value above is Apache/2.2.14 (Unix)|
The following is a simple CGI script that outputs CGI environment variables:
#!/usr/bin/python # -*- coding: UTF-8 -*- # filename:test.py import os print "Content-type: text/html" print print "<meta charset=\"utf-8\">" print "<b>Environmental variable</b><br>"; print "<ul>" for key in os.environ.keys(): print "<li><span style='color:green'>%30s </span> : %s </li>" % (key,os.environ[key]) print "</ul>"
Save the above point as test.py and modify the file permissions to 755. The results are as follows:
GET and POST methods
The browser client passes information to the server in two ways, the GET method and the POST method.
Transfer data using the GET method
The GET method sends the encoded user information to the server. The data information is included in the URL of the request page, separated by a "?", as follows:
http://www .test.com/cgi-bin/hello.py?key1=value1&key2=value2Some other notes about GET requests:
- GET requests can be cached
- GET requests remain in browser history
- GET requests can be bookmarked
- GET requests should not be used when processing sensitive data
- GET request has a length limit
- GET requests should only be used to retrieve data
Simple url instance: GET method
The following is a simple URL that uses the GET method to send two parameters to the hello_get.py program:
/cgi-bin/test.py?name=Novice Tutorial &url=http://www.welookups.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 # create FieldStorage CGI processing moduleCGI processing module form = cgi.FieldStorage() # retrieve data site_name = form.getvalue('name') site_url = form.getvalue('url') print "Content-type:text/html" print print "<html>" print "<head>" print "<meta charset=\"utf-8\">" print "<title>Novice tutorial CGI test instance</title>" print "</head>" print "<body>" print "<h2>%s：%s</h2>" % (site_name, site_url) Official websiteprint "</body>"< Span class="pln"> print "</html>" Pre>
Modify hello_get.py after saving the file, and modify the file permissions to 755:...chmod 755 hello_get.py
Browser request output:
The following is a form that uses the GET method to send two data to the server via the HTML form. The submitted server script is also the hello_get.py file. The hello_get.html code is as follows:<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>welookups Tutorial (welookups.com)</ Title> </head> <body> <form action="/cgi-bin/hello_get.py" method ="get"> Site Name: <input type="text" name ="name"> <br /> Site URL: <input type="text" name ="url" /> ; <input type="submit" value="Submit" /> Span> </form> </body> </html>
By default, the cgi-bin directory can only hold script files, we store hello_get.html in the test directory. Next, modify the file permissions to 755:chmod 755 hello_get Span>.html
The Gif demo is as follows:
Transfer data using the POST method
Using the POST method to pass data to the server is more secure and reliable. Some sensitive information such as user passwords need to use POST to transfer data.
The following is also hello_get.py, which can also handle POST form data submitted by the browser:#!/usr/bin/python # -*- coding: UTF-8 -*- # CGI processing module import cgi, cgitb # create FieldStorage Instantiation form = cgi.FieldStorage() # retrieve data site_name = form.getvalue('name') site_url = form.getvalue('url') print "Content-type:text/html" print print "<html>" print "<head>" print "<meta charset=\"utf-8\">" print "<title>Novice tutorial CGI test instance</title>" print "</head>" print "<body>" print "<h2>%sOfficial website：%s</h2>" % (site_name, site_url) print "</body>" print "</html>"
The following form submits data to the server script hello_get.py via the POST method (method="post"):<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> WELOOKUPS</title> </head> <body> <form action="/cgi-bin/hello_get.py" method="post"> Site name: <input type="text" name="name"> <br /> Site URL: <input type="text" name="url" /> <input type="submit" value="submit" /> </form> </body> </html>
The Gif demo is as follows: