Python_012 CGI编程
2019独角兽企业重金招聘Python工程师标准>>>
CGI架构图(Common Gateway Interface)
Web服务器支持及配置
在你进行CGI编程前,确保您的Web服务器支持CGI及已经配置了CGI的处理程序。
所有的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 NoneOptions ExecCGIOrder allow,denyAllow from all
</Directory><Directory "/var/www/cgi-bin">
Options All
</Directory>
第一个CGI程序
我们使用Python创建第一个CGI程序,文件名为hellp.py,文件位于/var/www/cgi-bin目录中,内容如下,修改文件的权限为755:
#coding=utf-8
#!/usr/bin/pythonprint "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>'
以上程序在浏览器访问显示结果如下:
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头部经常使用的信息:
头 | 描述 |
---|---|
Content-type: | 请求的与实体对应的MIME信息。例如: Content-type:text/html |
Expires: Date | 响应过期的日期和时间 |
Location: URL | 用来重定向接收方到非请求URL的位置来完成请求或标识新的资源 |
Last-modified: Date | 请求资源的最后修改时间 |
Content-length: N | 请求的内容长度 |
Set-Cookie: String | 设置Http Cookie |
CGI环境变量
所有的CGI程序都接收以下的环境变量,这些变量在CGI程序中发挥了重要的作用:
变量名 | 描述 |
---|---|
CONTENT_TYPE | 这个环境变量的值指示所传递来的信息的MIME类型。目前,环境变量CONTENT_TYPE一般都是:application/x-www-form-urlencoded,他表示数据来自于HTML表单。 |
CONTENT_LENGTH | 如果服务器与CGI程序信息的传递方式是POST,这个环境变量即使从标准输入STDIN中可以读到的有效数据的字节数。这个环境变量在读取所输入的数据时必须使用。 |
HTTP_COOKIE | 客户机内的 COOKIE 内容。 |
HTTP_USER_AGENT | 提供包含了版本数或其他专有数据的客户浏览器信息。 |
PATH_INFO | 这个环境变量的值表示紧接在CGI程序名之后的其他路径信息。它常常作为CGI程序的参数出现。 |
QUERY_STRING | 如果服务器与CGI程序信息的传递方式是GET,这个环境变量的值即使所传递的信息。这个信息经跟在CGI程序名的后面,两者中间用一个问号'?'分隔。 |
REMOTE_ADDR | 这个环境变量的值是发送请求的客户机的IP地址,例如上面的192.168.1.67。这个值总是存在的。而且它是Web客户机需要提供给Web服务器的唯一标识,可以在CGI程序中用它来区分不同的Web客户机。 |
REMOTE_HOST | 这个环境变量的值包含发送CGI请求的客户机的主机名。如果不支持你想查询,则无需定义此环境变量。 |
REQUEST_METHOD | 提供脚本被调用的方法。对于使用 HTTP/1.0 协议的脚本,仅 GET 和 POST 有意义。 |
SCRIPT_FILENAME | CGI脚本的完整路径 |
SCRIPT_NAME | CGI脚本的的名称 |
SERVER_NAME | 这是你的 WEB 服务器的主机名、别名或IP地址。 |
SERVER_SOFTWARE | 这个环境变量的值包含了调用CGI程序的HTTP服务器的名称和版本号。例如,上面的值为Apache/2.2.14(Unix) |
以下是一个简单的CGI脚本输出CGI的环境变量:
#coding=utf-8
#!/usr/bin/pythonimport osprint "Content-type: text/html\r\n\r\n";
print "<font size=+1>Environment</font><\br>";
for param in print "<b>%20s</b>: %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文件的代码:
#coding=utf-8
#!/usr/bin/python# CGI处理模块
import cgi, cgitb # 创建 FieldStorage 的实例化# 获取数据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>"
浏览器请求输出结果:
Hello ZARA ALI
简单的表单实例:GET方法
以下是一个通过HTML的表单使用GET方法向服务器发送两个数据,提交的服务器脚本同样是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>
使用POST方法传递数据
使用POST方法向服务器传递数据是更安全可靠的,像一些敏感信息如用户密码等需要使用POST传输数据。
以下同样是hello_get.py ,它也可以处理浏览器提交的POST表单数据:
#coding=utf-8
#!/usr/bin/python# 引入 CGI 模块
import cgi, cgitb # 创建 FieldStorage 实例# 获取表单数据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>"
以下为表单通过POST方法向服务器脚本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>
通过CGI程序传递checkbox数据
checkbox用于提交一个或者多个选项数据,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 文件的代码:
#coding=utf-8
#!/usr/bin/python# 引入 CGI 处理模块 # 创建 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 "<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>"
通过CGI程序传递Radio数据
Radio只向服务器传递一个数据,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 脚本代码如下:
#coding=utf-8
#!/usr/bin/python# 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>"
通过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脚本代码如下:
#coding=utf-8
#!/usr/bin/python# 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>"
通过CGI程序传递下拉数据
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 脚本代码如下所示:
#coding=utf-8
#!/usr/bin/python# 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>"
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:
<pre>
#coding=utf-8
#!/usr/bin/pythonprint "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信息的程序:
#coding=utf-8
#!/usr/bin/python# Import modules for CGI handling
from os import environ
import cgi, cgitbif environ.has_key('HTTP_COOKIE'):(key, value ) = split(cookie, '=');if key == "UserID":user_id = valueif key == "Password":password = valueprint "User ID = %s" % user_id
print "Password = %s" % password
以上脚本输出结果如下:
User ID = XYZ
Password = XYZ123
文件上传实例:
HTML设置上传文件的表单需要设置enctype 属性为multipart/form-data,代码如下所示:
<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脚本文件代码如下:
#coding=utf-8
#!/usr/bin/pythonimport cgi, os
import cgitb; cgitb.enable()form = cgi.FieldStorage()# 获取文件名# 检测文件是否上传
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
<html>
<body><p>%s</p>
</body>
</html>
""" % (message,)
如果你使用的系统是Unix/Linux,你必须替换文件分隔符,在window下只需要使用open()语句即可:
fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
文件下载对话框
如果我们需要为用户提供文件下载链接,并在用户点击链接后弹出文件下载对话框,我们通过设置HTTP头信息来实现这些功能,功能代码如下:
#coding=utf-8
#!/usr/bin/pythonprint "Content-Type:application/octet-stream; name=\"FileName\"\r\n";
print "Content-Disposition: attachment; filename=\"FileName\"\r\n\n";# Actual File Content will go hear.
fo = open("foo.txt", "rb")str = fo.read();
print str# Close opend file
fo.close()
转载于:https://my.oschina.net/u/2317401/blog/379294
Python_012 CGI编程相关推荐
- python cgitb_python CGI 编程实践
文章更新于:2020-03-05 一.Web服务器的支持与配置 在进行CGI编程之前,请确保Web服务器支持CGI,它被配置为处理CGI程序.所有对由HTTP服务器执行的CGI程序保存在一个预先配置的 ...
- Python基础教程(十):CGI编程、MySQL数据库
Python CGI编程 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如: ...
- html网页和cgi程序编程,CGI 编程方式学习
1.大家都知道CGI是通用网关接口,可以用来编写动态网页.而且CGI可以用很多种语言来写,用perl来编写最常见, 我这里就是用perl来编写做例子.讲到编写CGI编程方式,编写CGI有两程编程风格. ...
- android 如何读取cgi_Python基础教程(十):CGI编程、MySQL数据库
Python CGI编程 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如: ...
- python Web 之基石 - - CGI 编程 (基于python3)
一 CGI 编程之前缀篇 1CGI 入门简介 2Apache2 的安装以及配置运行环境搭建 二 开始 CGI programming 之旅 HTTP相关知识讲述 - - - first demo 随后 ...
- python cgi库_《Python 数据库 GUI CGI编程》
1. 上次写在前面,我们介绍了一篇关于开始使用Python.今天我们将介绍Python数据库,GUI, CGI编程和Python和Python的区别.2.连接到数据库标准Python数据库接口是Pyt ...
- Python CGI 编程 | 类FieldStorage的使用
使用Python自带的cgi库,可以很容易的实现CGI编程. 下面的例子实现了使用 类FieldStorage 得到POST或GET参数的方法 表单示例 <form method="P ...
- Python CGI编程
Python CGI编程 CGI是什么? 通用网关接口或CGI,是一组定义信息如何在Web服务器和自定义脚本之间交换的标准. CGI规范目前保持是由NCSA 和 NCSA 维护和定义如下. 通用网关接 ...
- [转载] 第一个Python CGI编程和配置
参考链接: Python中的CGI编程 1.什么是CGI CGI(Common Gateway Interface),通用网关接口,它是一段运行在服务器上的程序.CGI程序可以是Python脚本.PE ...
最新文章
- HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查...
- Eclipse 代码提示功能设置
- 学霸网站-Beta版本发布说明
- C语言试题七十五之请编写函数求回文数
- Linux 命令之 unset -- 删除指定的shell变量或函数
- 一个带关闭按钮的Div窗口,很漂亮
- 检查xml文件中包含非法xml字符的个数(
- HACMP环境修改IP的方法
- 如何在Linux中查看所有正在运行的进程 1
- 《Algorithms》Comparable 实现归并排序
- 知识图谱组队学习Task05——图数据库查询
- d - 数据结构实验之查找四:二分查找_数据结构与算法笔记
- Java内存五大区_一:jvm的五大内存区(内存结构)
- 一张图了解软件测试流程
- BFS算法解题套路框架(附习题)
- 突发,拼多多发生重大变更!
- Linux源码解析--从开机加电到main函数
- PXE网络启动 windows PE (使用微软官方工具)
- A survey of network-based intrusion detection data sets翻译二(5-8)
- 2022-2028年版全球及中国涂料市场销售运营模式及发展前景预测报告
热门文章
- 学习vue3系列watch
- java 线程栈大小配置,JVM运行时数据区详解-Stack栈(优化配置、代码样例)
- xp系统打开计算机硬盘分区,XP系统下怎么进行硬盘分区啊?
- mysql强制指定索引_mysql强制索引和禁止某个索引
- 笑脸哭脸循环c语言,买卖信号(哭脸笑脸)选股公式(源码)
- 给开源项目贡献代码的经历
- 20191004:包装类Integer,int,String类的相互转换
- jdk tomcat mysql配置_Linux jdk、tomcat、mysql配置
- mysql查询字段转型_mysql转型
- oracle 创建视图、修改视图、删除视图、利用视图操作基本表