Python 简明教程

Python - CGI Programming

通用网关接口 (CGI) 是一组标准,定义了 Web 服务器和自定义脚本来交换信息的规则。CGI 规范目前由 NCSA 负责维护。

What is CGI?

  1. 通用网关接口 (CGI) 是外部网关程序与诸如 HTTP 服务器等信息服务器进行交互的标准。

  2. 当前版本为 CGI/1.1,CGI/1.2 正在进行中。

Web Browsing

为了理解 CGI 的概念,让我们来看看当我们点击超链接来浏览特定的网页或 URL 时会发生什么。

  1. 浏览器联系 HTTP 网页服务器并请求 URL,即文件名。

  2. 网页服务器解析 URL 并查找文件名。如果找到该文件,则将其发送回浏览器,否则发送错误消息,表明请求的是错误的文件。

  3. 网页浏览器接受网页服务器的响应并显示收到的文件或错误消息。

但是,可以设置 HTTP 服务器,以便只要请求特定目录中的文件,就不会发回该文件;而是将其作为程序执行,并且该程序输出的任何内容都会发回以供浏览器显示。此功能称为通用网关接口或 CGI,并且这些程序称为 CGI 脚本。这些 CGI 程序可以是 Python 脚本、PERL 脚本、Shell 脚本、C 或 C++ 程序等。

CGI Architecture Diagram

cgi architecture diagram

Web Server Support and Configuration

在继续进行 CGI 编程之前,请确保你的网页服务器支持 CGI,并且它配置为处理 CGI 程序。所有要由 HTTP 服务器执行的 CGI 程序都保存在预先配置的目录中。此目录称为 CGI 目录,按惯例将其命名为 /var/www/cgi-bin。按惯例,CGI 文件的扩展名为 cgi, ,但你也可以使用 python 扩展名 .py 保留你的文件。

默认情况下,Linux 服务器配置为仅运行 /var/www 中 cgi-bin 目录中的脚本。如果你想指定任何其他目录来运行 CGI 脚本,请在 httpd.conf 文件中注释掉以下行:

<Directory "/var/www/cgi-bin">
   AllowOverride None
   Options ExecCGI
   Order allow,deny
   Allow from all
</Directory>

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

还应添加以下行为 apache 服务器添加,以将 .py 文件视为 cgi 脚本。

AddHandler cgi-script .py

在此,我们假设你的网页服务器已成功启动并运行,并且你可以运行任何其他 CGI 程序,如 Perl 或 Shell 等。

First CGI Program

这里有一个简单的链接,它链接到一个名为 hello.py 的 CGI 脚本。此文件保存在 /var/www/cgi-bin 目录中,其内容如下。在运行 CGI 程序之前,请确保使用 chmod 755 hello.py UNIX 命令更改文件的模式,使其可执行。

print ("Content-type:text/html\r\n\r\n")
print ('<html>')
print ('<head>')
print ('<title>Hello Word - First CGI Program</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! This is my first CGI program</h2>')
print ('</body>')
print ('</html>')

Note − 脚本中的第一行必须是 Python 可执行文件的路径。它在 Python 程序中显示为注释,但它被称为井号行。

在 Linux 中,它应该是 #!/usr/bin/python3。

在 Windows 中,它应该是 #!c:/python311/python.exd。

在浏览器中输入以下 URL:

http://localhost/cgi-bin/hello.py
Hello Word! This is my first CGI program

此 hello.py 脚本是一个简单的 Python 脚本,它在 STDOUT 文件(即屏幕)上写入其输出。有一个重要且额外的可用功能,即第一个要打印的行 Content-type:text/html\r\n\r\n 。此行发回浏览器,它指定要在浏览器屏幕上显示的内容类型。

现在你肯定已经理解了 CGI 的基本概念,并且你可以使用 Python 编写许多复杂的 CGI 程序。此脚本还可以与任何其他外部系统交互,以交换诸如 RDBMS 之类的信息。

HTTP Header

Content-type:text/html\r\n\r\n 是发送到浏览器的 HTTP 头的一部分,用于理解内容。所有 HTTP 头都将采用以下形式:

HTTP Field Name: Field Content

For Example
Content-type: text/html\r\n\r\n

有其他一些重要的 HTTP 头信息,您将在 CGI 编程中经常使用。

CGI Environment Variables

所有的 CGI 程序都可以访问以下环境变量。这些变量在编写任何 CGI 程序时都扮演着重要的角色。

这里有一个列出所有 CGI 变量的小型 CGI 程序。单击此链接以查看结果 @ {s0}

import os

print ("Content-type: text/html\r\n\r\n");
print ("<font size=+1>Environment</font><\br>");
for param in os.environ.keys():
   print ("<b>%20s</b>: %s<\br>" % (param, os.environ[param]))

GET and POST Methods

当您需要将一些信息从浏览器传递到 Web 服务器以及最终传递到 CGI 程序时,您肯定碰到过很多情况。浏览器通常使用两种方法向 Web 服务器传递此信息。这些方法分别是 GET 方法和 POST 方法。

Passing Information using GET method

GET 方法将编码后的用户信息附加到页面请求后发送。页面和编码后的信息由 ? 字符分隔,如下所示:

http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2
  1. GET 方法是从浏览器向 Web 服务器传递信息的默认方法,并且它会生成一个长字符串,该字符串出现在浏览器的“位置:”框中。

  2. 如果您有密码或其他敏感信息要传递到服务器,请不要使用 GET 方法。

  3. GET 方法具有大小限制:在一个请求字符串中只能发送 1024 个字符。

  4. GET 方法使用 QUERY_STRING 头发送信息,并且可以通过 QUERY_STRING 环境变量在您的 CGI 程序中访问。

您可以通过简单地连接键和值对以及任意 URL 来传递信息,或者可以使用 HTML <FORM> 标记使用 GET 方法传递信息。

Simple URL Example:Get Method

这是一个简单的 URL,它使用 GET 方法将两个值传递到 hello_get.py 程序。

/cgi-bin/hello_get.py?first_name=Malhar&last_name=Lathkar

以下是用于处理 Web 浏览器给出的输入的 hello_get.py 脚本。我们将使用 CGI 模块,它使得访问传递的信息变得非常容易。

# Import modules for CGI handling
import cgi, cgitb

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

# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')

print ("Content-type:text/html")
print()
print ("<html>")
print ('<head>')
print ("<title>Hello - Second CGI Program</title>")
print ('</head>')
print ('<body>')
print ("<h2>Hello %s %s</h2>" % (first_name, last_name))
print ('</body>')
print ('</html>')

这将生成以下结果:

Hello Malhar Lathkar

Simple FORM Example:GET Method

此示例使用 HTML 表单和提交按钮传递两个值。我们使用相同的 CGI 脚本 hello_get.py 来处理此输入。

<form action = "/cgi-bin/hello_get.py" method = "get">
   First Name: <input type = "text" name = "first_name">  <br />

   Last Name: <input type = "text" name = "last_name" />
   <input type = "submit" value = "Submit" />
</form>

这是上述表单的实际输出,您输入名字和姓氏,然后单击提交按钮查看结果。

Passing Information Using POST Method

向 CGI 程序传递信息的一个通常更为可靠的方法是 POST 方法。此方法将信息打包的方式与 GET 方法完全相同,但是不是在 URL 中的 ? 之后作为文本字符串发送信息,而是作为单独的消息发送信息。此消息以标准输入的形式进入 CGI 脚本。

以下相同的 hello_get.py 脚本同时处理 GET 和 POST 方法。

# Import modules for CGI handling
import cgi, cgitb

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

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - Second CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello %s %s</h2>" % (first_name, last_name)
print "</body>"
print "</html>"

让我们再次举一个与上述相同的示例,它使用 HTML 表单和提交按钮传递两个值。我们使用相同的 CGI 脚本 hello_get.py 来处理此输入。

<form action = "/cgi-bin/hello_get.py" method = "post">
First Name: <input type = "text" name = "first_name"><br />
Last Name: <input type = "text" name = "last_name" />

<input type = "submit" value = "Submit" />
</form>

这是上述表单的实际输出。您输入名字和姓氏,然后单击提交按钮查看结果。

Passing Checkbox Data to CGI Program

如果要求选择多个选项,则使用复选框。

这是一个包含两个复选框的表单的示例 HTML 代码−

<form action = "/cgi-bin/checkbox.cgi" method = "POST" target = "_blank">
   <input type = "checkbox" name = "maths" value = "on" /> Maths
   <input type = "checkbox" name = "physics" value = "on" /> Physics
   <input type = "submit" value = "Select Subject" />
</form>

这段代码的结果是以下表单−

以下是 checkbox.cgi 脚本,用于处理由网络浏览器针对复选框按钮提供的输入。

# Import modules for CGI handling
import cgi, cgitb

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

# Get data from fields
if form.getvalue('maths'):
   math_flag = "ON"
else:
   math_flag = "OFF"

if form.getvalue('physics'):
   physics_flag = "ON"
else:
   physics_flag = "OFF"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Checkbox - Third CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> CheckBox Maths is : %s</h2>" % math_flag
print "<h2> CheckBox Physics is : %s</h2>" % physics_flag
print "</body>"
print "</html>"

Passing Radio Button Data to CGI Program

如果要求只选择一个选项,则使用单选按钮。

这是一个包含两个单选按钮的表单的示例 HTML 代码−

<form action = "/cgi-bin/radiobutton.py" method = "post" target = "_blank">
   <input type = "radio" name = "subject" value = "maths" /> Maths
   <input type = "radio" name = "subject" value = "physics" /> Physics
   <input type = "submit" value = "Select Subject" />
</form>

这段代码的结果是以下表单−

下方是 radiobutton.py 脚本,它用于处理由 Web 浏览器针对单选按钮提供的输入 −

# Import modules for CGI handling
import cgi, cgitb

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

# Get data from fields
if form.getvalue('subject'):
   subject = form.getvalue('subject')
else:
   subject = "Not set"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Radio - Fourth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"

Passing Text Area Data to CGI Program

如果必须将多行文本传递给 CGI 程序,则使用 TEXTAREA 元素。

这是一个包含一个 TEXTAREA 框的表单的示例 HTML 代码−

<form action = "/cgi-bin/textarea.py" method = "post" target = "_blank">
   <textarea name = "textcontent" cols = "40" rows = "4">
      Type your text here...
   </textarea>
   <input type = "submit" value = "Submit" />
</form>

这段代码的结果是以下表单−

以下是 textarea.cgi 脚本,它用于处理由 Web 浏览器提供的输入 −

# Import modules for CGI handling
import cgi, cgitb

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

# Get data from fields
if form.getvalue('textcontent'):
   text_content = form.getvalue('textcontent')
else:
   text_content = "Not entered"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>";
print "<title>Text Area - Fifth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Entered Text Content is %s</h2>" % text_content
print "</body>"

Passing Drop Down Box Data to CGI Program

当我们有许多可用选项,但只选择一个或两个时,可以使用下拉框。

这是一个包含一个下拉框的表单的示例 HTML 代码−

<form action = "/cgi-bin/dropdown.py" method = "post" target = "_blank">
   <select name = "dropdown">
      <option value = "Maths" selected>Maths</option>
      <option value = "Physics">Physics</option>
   </select>
   <input type = "submit" value = "Submit"/>
</form>

这段代码的结果是以下表单−

以下是 dropdown.py 脚本,它用于处理由 Web 浏览器提供的输入。

# Import modules for CGI handling
import cgi, cgitb

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

# Get data from fields
if form.getvalue('dropdown'):
   subject = form.getvalue('dropdown')
else:
   subject = "Not entered"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Dropdown Box - Sixth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"

Using Cookies in CGI

HTTP 协议是无状态协议。对于商业网站来说,必须在不同的页面之间保持会话信息。例如,一个用户注册在完成多个页面后结束。如何跨所有网页维护用户的会话信息?

在许多情况下,使用 cookie 是记忆和跟踪偏好、购买、佣金和其他信息最有效的方法,而这些信息对于提升访问者体验或网站统计至关重要。

How It Works?

您的服务器会以 Cookie 的形式向访客的浏览器发送一些数据。浏览器可能会接受 Cookie。如果接受,它将作为纯文本记录存储在访客硬盘上。现在,当访客到达您站点上的另一个页面时,可以检索 Cookie。检索后,您的服务器知道/记住存储的内容。

Cookie 是纯文本数据记录,包含 5 个可变长度字段:

  1. Expires − cookie 的过期日期。如果留空,则 cookie 将在访问者退出浏览器时过期。

  2. Domain − 您网站的域名。

  3. Path − 设置 Cookie 的目录或网页的路径。如果您想从任何目录或页面检索 Cookie,则此项可能为空。

  4. Secure − 如果此字段包含单词“安全”,那么只能使用安全服务器检索 cookie。如果此字段留空,则不存在此类限制。

  5. Name = Value − Cookie 以键值对的形式设置和检索。

Setting up Cookies

将 Cookie 发送到浏览器非常容易。这些 Cookie 与内容类型字段之前的 HTTP 标头一起发送。假设您希望将 UserID 和密码设置为 Cookie。设置 Cookie 如下所示 −

print "Set-Cookie:UserID = XYZ;\r\n"
print "Set-Cookie:Password = XYZ123;\r\n"
print "Set-Cookie:Expires = Tuesday, 31-Dec-2007 23:12:40 GMT;\r\n"
print "Set-Cookie:Domain = www.tutorialspoint.com;\r\n"
print "Set-Cookie:Path = /perl;\n"
print "Content-type:text/html\r\n\r\n"
...........Rest of the HTML Content....

从这个示例中,您应该已经了解了如何设置 Cookie。我们使用 Set-Cookie HTTP 标头设置 Cookie。

设置 Expires、Domain 和 Path 等 cookie 属性是可选的。需要注意的是,在发送 magci 行 "Content-type:text/html\r\n\r\n 之前设置 cookie。

Retrieving Cookies

检索所有设置的 cookie 非常容易。Cookie 存储在 CGI 环境变量 HTTP_COOKIE 中,它们将具有以下形式:

key1 = value1;key2 = value2;key3 = value3....

以下是有关如何检索 cookie 的示例。

# Import modules for CGI handling
from os import environ
import cgi, cgitb

if environ.has_key('HTTP_COOKIE'):
   for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):
      (key, value ) = split(cookie, '=');
      if key == "UserID":
         user_id = value

      if key == "Password":
         password = value

print "User ID  = %s" % user_id
print "Password = %s" % password

这会为上述脚本设置的 cookie 产生以下结果:

User ID = XYZ
Password = XYZ123

File Upload Example

要上传文件,HTML 表单必须将 enctype 属性设置为 multipart/form-data 。具有 file 类型的 input 标记创建一个“浏览”按钮。

<html>
   <body>
      <form enctype = "multipart/form-data" action = "save_file.py" method = "post">
      <p>File: <input type = "file" name = "filename" /></p>
      <p><input type = "submit" value = "Upload" /></p>
      </form>
   </body>
</html>

这段代码的结果是以下表单−

上述示例已故意禁用,以防止用户在我们的服务器上上传文件,但是您可以在自己的服务器上尝试上述代码。

以下是处理文件上传的脚本 save_file.py

import cgi, os
import cgitb; cgitb.enable()

form = cgi.FieldStorage()

# Get filename here.
fileitem = form['filename']

# Test if the file was uploaded
if fileitem.filename:
   # strip leading path from file name to avoid
   # directory traversal attacks
   fn = os.path.basename(fileitem.filename)
   open('/tmp/' + fn, 'wb').write(fileitem.file.read())

   message = 'The file "' + fn + '" was uploaded successfully'

else:
   message = 'No file was uploaded'

print """\
Content-Type: text/html\n
<html>
   <body>
      <p>%s</p>
   </body>
</html>
""" % (message,)

如果您在 Unix/Linux 上运行上述脚本,则需要小心更换文件分隔符,如下所示,否则在您的 Windows 机器上,上述 open() 语句应正常工作。

fn = os.path.basename(fileitem.filename.replace("\\", "/" ))

How To Raise a "File Download" Dialog Box?

有时,您希望提供一个选项,用户可以单击一个链接,并向用户弹出“文件下载”对话框,而不是显示实际内容。这非常容易,可以通过 HTTP 头来实现。这个 HTTP 头与前一部分中提到的头不同。

例如,如果您希望从给定链接下载 FileName 文件,则其语法如下:

# HTTP Header
print "Content-Type:application/octet-stream; name = \"FileName\"\r\n";
print "Content-Disposition: attachment; filename = \"FileName\"\r\n\n";

# Actual File Content will go here.
fo = open("foo.txt", "rb")

str = fo.read();
print str

# Close opend file
fo.close()