Python CGI编程

什么是CGI
CGI 目前由NCSA维护,NCSA定义CGI如下:

CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户端HTML页面的接口。

网页浏览
为了更好的了解CGI是如何工作的,我们可以从在网页上点击一个链接或URL的流程:

1、使用你的浏览器访问URL并连接到HTTP web 服务器。
2、Web服务器接收到请求信息后会解析URL,并查找访问的文件在服务器上是否存在,如果存在返回文件的内容,否则返回错误信息。
3、浏览器从服务器上接收信息,并显示接收的文件或者错误信息。
CGI程序可以是Python脚本,PERL脚本,SHELL脚本,C或者C++程序等。

CGI架构图
cgiarch

Web服务器支持及配置
在你进行CGI编程前,确保您的Web服务器支持CGI及已经配置了CGI的处理程序。

Apache 支持CGI 配置:

设置好CGI目录:

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
所有的HTTP服务器执行CGI程序都保存在一个预先配置的目录。这个目录被称为CGI目录,并按照惯例,它被命名为/var/www/cgi-bin目录。

CGI文件的扩展名为.cgi,python也可以使用.py扩展名。

默认情况下,Linux服务器配置运行的cgi-bin目录中为/var/www。

如果你想指定其他运行CGI脚本的目录,可以修改httpd.conf配置文件,如下所示:

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

在 AddHandler 中添加 .py 后缀,这样我们就可以访问 .py 结尾的 python 脚本文件:

AddHandler cgi-script .cgi .pl .py
第一个CGI程序
我们使用Python创建第一个CGI程序,文件名为hellp.py,文件位于/var/www/cgi-bin目录中,内容如下,修改文件的权限为755:

#!/usr/bin/python

-- coding: UTF-8 --

print “Content-type:text/html\r\n\r\n”
print ‘’
print ‘’
print ‘

Hello Word - First CGI Program
print ‘’
print ‘’
print ‘

Hello Word! This is my first CGI program


print ‘’
print ‘’
以上程序在浏览器访问显示结果如下:

Hello Word! This is my first CGI program
这个的hello.py脚本是一个简单的Python脚本,脚本第一行的输出内容"Content-type:text/html\r\n\r\n"发送到浏览器并告知浏览器显示的内容类型为"text/html"。

HTTP头部
hello.py文件内容中的" Content-type:text/html\r\n\r\n"即为HTTP头部的一部分,它会发送给浏览器告诉浏览器文件的内容类型。

HTTP头部的格式如下:

HTTP 字段名: 字段内容

例如
Content-type: text/html\r\n\r\n
以下表格介绍了CGI程序中HTTP头部经常使用的信息:Python CGI编程
CGI环境变量
所有的CGI程序都接收以下的环境变量,这些变量在CGI程序中发挥了重要的作用:Python CGI编程
以下是一个简单的CGI脚本输出CGI的环境变量:

#!/usr/bin/python

-- coding: UTF-8 --

import os

print “Content-type: text/html\r\n\r\n”;
print “Environment<\br>”;
for param in os.environ.keys():
print “%20s: %s<\br>” % (param, os.environ[param])
GET和POST方法
浏览器客户端通过两种方法向服务器传递信息,这两种方法就是 GET 方法和 POST 方法。

使用GET方法传输数据
GET方法发送编码后的用户信息到服务端,数据信息包含在请求页面的URL上,以"?"号分割, 如下所示:

http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2
有关 GET 请求的其他一些注释:
GET 请求可被缓存
GET 请求保留在浏览器历史记录中
GET 请求可被收藏为书签
GET 请求不应在处理敏感数据时使用
GET 请求有长度限制
GET 请求只应当用于取回数据
简单的url实例:GET方法
以下是一个简单的URL,使用GET方法向hello_get.py程序发送两个参数:

/cgi-bin/hello_get.py?first_name=ZARA&last_name=ALI
以下为hello_get.py文件的代码:

#!/usr/bin/python

-- coding: UTF-8 --

CGI处理模块

import cgi, cgitb

创建 FieldStorage 的实例化

form = cgi.FieldStorage()

获取数据

first_name = form.getvalue(‘first_name’)
last_name = form.getvalue(‘last_name’)

print “Content-type:text/html\r\n\r\n”
print “”
print “”
print “

Hello - Second CGI Program
print “”
print “”
print “

Hello %s %s

” % (first_name, last_name)
print “”
print “”
浏览器请求输出结果:

Hello ZARA ALI
简单的表单实例:GET方法
以下是一个通过HTML的表单使用GET方法向服务器发送两个数据,提交的服务器脚本同样是hello_get.py文件,代码如下:

First Name:

Last Name:

使用POST方法传递数据 使用POST方法向服务器传递数据是更安全可靠的,像一些敏感信息如用户密码等需要使用POST传输数据。

以下同样是hello_get.py ,它也可以处理浏览器提交的POST表单数据:

#!/usr/bin/python

-- coding: UTF-8 --

引入 CGI 模块

import cgi, cgitb

创建 FieldStorage 实例

form = cgi.FieldStorage()

获取表单数据

first_name = form.getvalue(‘first_name’)
last_name = form.getvalue(‘last_name’)

print “Content-type:text/html\r\n\r\n”
print “”
print “”
print “

Hello - Second CGI Program
print “”
print “”
print “

Hello %s %s

” % (first_name, last_name)
print “”
print “”
以下为表单通过POST方法向服务器脚本hello_get.py提交数据:

First Name:
Last Name: 通过CGI程序传递checkbox数据 checkbox用于提交一个或者多个选项数据,HTML代码如下: Maths Physics 以下为 checkbox.cgi 文件的代码:

#!/usr/bin/python

-- coding: UTF-8 --

引入 CGI 处理模块

import cgi, cgitb

创建 FieldStorage的实例

form = cgi.FieldStorage()

接收字段数据

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 “”
print “”
print “

Checkbox - Third CGI Program
print “”
print “”
print "

CheckBox Maths is : %s

" % math_flag
print "

CheckBox Physics is : %s

" % physics_flag
print “”
print “”
通过CGI程序传递Radio数据
Radio只向服务器传递一个数据,HTML代码如下:

Maths Physics radiobutton.py 脚本代码如下:

#!/usr/bin/python

-- coding: UTF-8 --

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 “”
print “”
print “

Radio - Fourth CGI Program
print “”
print “”
print "

Selected Subject is %s

" % subject
print “”
print “”
通过CGI程序传递 Textarea 数据
Textarea向服务器传递多行数据,HTML代码如下:

Type your text here... textarea.cgi脚本代码如下:

#!/usr/bin/python

-- coding: UTF-8 --

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 “”
print “”;
print “

Text Area - Fifth CGI Program
print “”
print “”
print "

Entered Text Content is %s

" % text_content
print “”
通过CGI程序传递下拉数据
HTML下拉框代码如下:

Maths Physics dropdown.py 脚本代码如下所示:

#!/usr/bin/python

-- coding: UTF-8 --

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 “”
print “”
print “

Dropdown Box - Sixth CGI Program
print “”
print “”
print "

Selected Subject is %s

" % subject
print “”
print “”
CGI中使用Cookie
在http协议一个很大的缺点就是不作用户身份的判断,这样给编程人员带来很大的不便,

而cookie功能的出现弥补了这个缺憾。
所有cookie就是在客户访问脚本的同时,通过客户的浏览器,在客户硬盘上写入纪录数据 ,当下次客户访问脚本时取回数据信息,从而达到身份判别的功能,cookie常用在密码判断中 。

cookie的语法
http cookie的发送是通过http头部来实现的,他早于文件的传递,头部set-cookie的语法如下:

Set-cookie:name=name;expires=date;path=path;domain=domain;secure
name=name: 需要设置cookie的值(name不能使用";“和”,“号),有多个name值时用”;“分隔例如:name1=name1;name2=name2;name3=name3。
expires=date: cookie的有效期限,格式: expires=“Wdy,DD-Mon-YYYY HH:MM:SS”
path=path: 设置cookie支持的路径,如果path是一个路径,则cookie对这个目录下的所有文件及子目录生效,例如: path=”/cgi-bin/",如果path是一个文件,则cookie指对这个文件生效,例如:path="/cgi-bin/cookie.cgi"。
domain=domain: 对cookie生效的域名,例如:domain=“www.chinalb.com”
secure: 如果给出此标志,表示cookie只能通过SSL协议的https服务器来传递。
cookie的接收是通过设置环境变量HTTP_COOKIE来实现的,CGI程序可以通过检索该变量获取cookie信息。
Cookie设置
Cookie的设置非常简单,cookie会在http头部单独发送。以下实例在cookie中设置了UserID 和 Password:

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

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.w3cschool.cc;\r\n"
print "Set-Cookie:Path=/perl;\n"
print "Content-type:text/html\r\n\r\n"
...........Rest of the HTML Content....
以上实例使用了 Set-Cookie 头信息来设置Cookie信息,可选项中设置了Cookie的其他属性,如过期时间Expires,域名Domain,路径Path。这些信息设置在 "Content-type:text/html\r\n\r\n"之前。

检索Cookie信息
Cookie信息检索页非常简单,Cookie信息存储在CGI的环境变量HTTP_COOKIE中,存储格式如下:

key1=value1;key2=value2;key3=value3....
以下是一个简单的CGI检索cookie信息的程序:

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

# 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
以上脚本输出结果如下:

User ID = XYZ
Password = XYZ123
文件上传实例:

HTML设置上传文件的表单需要设置enctype 属性为multipart/form-data,代码如下所示:



   
   

File:

save_file.py脚本文件代码如下: #!/usr/bin/python # -*- coding: UTF-8 -*- import cgi, os import cgitb; cgitb.enable() form = cgi.FieldStorage() # 获取文件名 fileitem = form['filename'] # 检测文件是否上传 if fileitem.filename: # 设置文件路径 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

%s

""" % (message,) 如果你使用的系统是Unix/Linux,你必须替换文件分隔符,在window下只需要使用open()语句即可: fn = os.path.basename(fileitem.filename.replace("\\", "/" )) 文件下载对话框 我们先在当前目录下创建 foo.txt 文件,用于程序的下载。 文件下载通过设置HTTP头信息来实现,功能代码如下: #!/usr/bin/python # -*- coding: UTF-8 -*- # HTTP 头部 print "Content-Disposition: attachment; filename=\"foo.txt\"\r\n\n"; # 打开文件 fo = open("foo.txt", "rb") str = fo.read(); print str # 关闭文件 fo.close()