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


Web browsing

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-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 Programming tutorial First CGI program </h2>'
print '</body>'
print '</html>'

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

chmod 755 hello.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 Programming 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.


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

HeadDescription
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: DateReserved resource last modified time
Content-length: NRequested 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:

variable namedescription
CONTENT_TYPEThe 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_LENGTHIf 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_COOKIECOOKIE content within the client.
HTTP_USER_AGENT Provides client browser information that includes the number of versions or other proprietary data.
PATH_INFOThe 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_STRINGIf 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_ADDRThe 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_FILENAMEFull path to CGI script
SCRIPT_NAMEThe name of the CGI script
SERVER_NAMEThis is the host name, alias or IP address of your web server.
SERVER_SOFTWAREThe 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=value2
Some 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>" 

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>Programming 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" />
</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.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>%s官网:%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:

passing checkbox data via CGI program

checkbox is used to submit one or more option data, the HTML code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WELOOKUPS.COM</title>
</head>
<body>
<form action="/cgi-bin/checkbox.py" method="POST" target="_blank">
<input type="checkbox" name="welookups" value="on" /> WELOOKUPS
<input type="checkbox" name="google" value="on" /> Google
<input type="submit" value="Select site" />
</form>
</body>
</html>

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# Introducing CGI Processing Modules import cgi, cgitb

# Create an instance of FieldStorage form = cgi.FieldStorage()

# Receive field dataif form.getvalue('google'):
google_flag = "Yes"
else:
google_flag = "no"

if form.getvalue('welookups'):
welookups_flag = "Yes"
else:
welookups_flag = "no"

Passing Radio Data via CGI Program

Radio only passes a single data to the server, the HTML code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>welookups.com</title>
</head>
<body>
<form action="/cgi-bin/radiobutton.py" method="post" target="_blank">
    
  <input type="radio" name="site" value="welookups" /> Programming tutorial
<input type="radio" name="site" value="google" /> Google
<input type="submit" value="submit" />
</form>
</body>
</html>
print "Content-type:text/html" print print "<html>" print "<head>" print "<meta charset=\"utf-8\">" print "<title>Programming tutorial CGI Test case</title>" print "</head>" print "<body>" print "<h2> The selected website is %s</h2>" % site print "</body>" print "</html>"

modify radiobutton.py Permission:

chmod 755 radiobutton.py

Browser access Gif demo:

passing Textarea data through CGI programs

Textarea passes multiple lines of data to the server. The HTML code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Programming tutorial(welookups.com)</title>
</head>
<body>
<form action="/cgi-bin/textarea.py" method="post" target="_blank">
<textarea name="textcontent" cols="40" rows="4">
Enter content here...</textarea>
<input type="submit" value="submit" />
</form>
</body>
</html>

textarea.py The script code is as follows:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# Introduce CGI Processing module 
import cgi, cgitb 

# Create an instance of FieldStorage
form = cgi.FieldStorage() 

# Receive field data
if form.getvalue('textcontent'):
   text_content = form.getvalue('textcontent')
else:
   text_content = "No content"


print "Content-type:text/html"
print
print "<html>"
print "<head>";
print "<meta charset=\"utf-8\">"
print "<title>Programming tutorial CGI Test case</title>"
print "</head>"
print "<body>"
print "<h2> modify...:%s</h2>" % text_content
print "</body>"
print "</html>"

modify textarea.py Permission:






welookups is optimized for learning.© welookups .
All Right Reserved and you agree to have read and accepted our term and condition