

[root@www cgi-bin]# cat test4.py#!/usr/bin/pythonimport cgiform = cgi.FieldStorage()name= form.getvalue(‘name‘,‘world‘)
print ‘Content-type: text/plain‘
print ‘hello,%s‘ % name[root@www cgi-bin]#
<!doctype html><html><head><title>hello python cgi</title></head><body><formaction="/cgi-bin/form.py"><labelfor="">username:</label><inputtype="text"name="username"value="username"><labelfor="">password:</label><inputtype="password"name="password"value=""><inputtype="submit"></form></body></html>
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cgi
header = ‘Content-Type: text/html\n\n‘
html = ‘<h3>接受处理表单数据\n</h3>‘
#打印返回的内容
print header
print html
# 接受表达提交的数据
form = cgi.FieldStorage()
print ‘接收表达get的数据 :‘,form
print ‘<p />‘
# 解析处理提交的数据
username = form[‘username‘].value #也可以username = form.getvalue(‘username‘)用getvalue获取
password = form[‘password‘].value
formhtml = ‘‘‘
<label for="">username:</label><input type="text" value="%s">
<label for="">password:</label><input type="text" value = "%s">
‘‘‘
print formhtml % (username,password)
[root@www cgi-bin]# cat test4.py#!/usr/bin/pythonimport cgiform = cgi.FieldStorage()name= form.getvalue(‘name‘,‘world‘)
print ‘Content-type: text/html‘
#print ‘hello,%s‘ % name
print """
<html><head><title>change name</title></head><body><h1>hello.%s!</h1><form action=‘test4.py‘>change name <input type=‘text‘ name=‘name‘ /><input type=‘submit‘/></form></body>
</html>
""" % name
[root@www cgi-bin]#
文件内容中的" Content-type:text/html\r\n\r\n"即为HTTP头部的一部分,它会发送给浏览器告诉浏览器文件的内容类型。
HTTP头部的格式如下:
HTTP 字段名: 字段内容 例如 Content-type: text/html\r\n\r\n
以下表格介绍了CGI程序中HTTP头部经常使用的信息:
| 头 | 描述 | 
|---|---|
| Content-type: | 请求的与实体对应的MIME信息。例如: Content-type:text/html | 
| Expires: Date | 响应过期的日期和时间 | 
| Location: URL | 用来重定向接收方到非请求URL的位置来完成请求或标识新的资源 | 
| Last-modified: Date | 请求资源的最后修改时间 | 
| Content-length: N | 请求的内容长度 | 
| Set-Cookie: String | 设置Http Cookie | 

replace this:
ScriptAlias /cgi-bin /var/www/html/cgi-bin/  
with  :
原文:http://blog.csdn.net/sxkjkoukou/article/details/31768243