微网站建设及微信推广方案ppt模板,电商公司有哪些?,wordpress封面图插件,站长工具站长之家Python CGI编程 -2
GET和POST方法
浏览器客户端通过两种方法向服务器传递信息#xff0c;这两种方法就是 GET 方法和 POST 方法。
使用GET方法传输数据
GET方法发送编码后的用户信息到服务端#xff0c;数据信息包含在请求页面的URL上#xff0c;以?号分割…Python CGI编程 -2
GET和POST方法
浏览器客户端通过两种方法向服务器传递信息这两种方法就是 GET 方法和 POST 方法。
使用GET方法传输数据
GET方法发送编码后的用户信息到服务端数据信息包含在请求页面的URL上以?号分割, 如下所示
http://www.test.com/cgi-bin/hello.py?key1value1key2value2有关 GET 请求的其他一些注释* GET 请求可被缓存
GET 请求保留在浏览器历史记录中GET 请求可被收藏为书签GET 请求不应在处理敏感数据时使用GET 请求有长度限制GET 请求只应当用于取回数据
简单的url实例GET方法
以下是一个简单的URL使用GET方法向hello_get.py程序发送两个参数
/cgi-bin/test.py?name菜鸟教程urlhttp://www.runoob.com以下为 hello_get.py 文件的代码
#!/usr/bin/python3# CGI处理模块
import cgi, cgitb# 创建 FieldStorage 的实例化
form cgi.FieldStorage()# 获取数据
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菜鸟教程 CGI 测试实例/title)
print (/head)
print (body)
print (h2%s官网%s/h2 % (site_name, site_url))
print (/body)
print (/html)文件保存后修改 hello_get.py修改文件权限为 755
chmod 755 hello_get.py浏览器请求输出结果 简单的表单实例GET方法
以下是一个通过HTML的表单使用GET方法向服务器发送两个数据提交的服务器脚本同样是hello_get.py文件hello_get.html 代码如下
# 实例
!DOCTYPE html
html
head
meta charsetutf-8
title菜鸟教程(runoob.com)/title
/head
body
form action/cgi-bin/hello_get.py methodget
站点名称: input typetext namename br /站点 URL: input typetext nameurl /
input typesubmit value提交 /
/form
/body
/html默认情况下 cgi-bin 目录只能存放脚本文件我们将 hello_get.html 存储在 test 目录下修改文件权限为 755
chmod 755 hello_get.htmlGif 演示如下所示 使用POST方法传递数据
使用POST方法向服务器传递数据是更安全可靠的像一些敏感信息如用户密码等需要使用POST传输数据。
以下同样是hello_get.py 它也可以处理浏览器提交的POST表单数据:
#!/usr/bin/python3# CGI处理模块
import cgi, cgitb# 创建 FieldStorage 的实例化
form cgi.FieldStorage()# 获取数据
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菜鸟教程 CGI 测试实例/title)
print (/head)
print (body)
print (h2%s官网%s/h2 % (site_name, site_url))
print (/body)
print (/html)以下为表单通过POST方法 method“post” 向服务器脚本 hello_get.py 提交数据:
!DOCTYPE html
html
head
meta charsetutf-8
title菜鸟教程(runoob.com)/title
/head
body
form action/cgi-bin/hello_get.py methodpost
站点名称: input typetext namename br /站点 URL: input typetext nameurl /
input typesubmit value提交 /
/form
/body
/html
/formGif 演示如下所示 通过CGI程序传递checkbox数据
checkbox用于提交一个或者多个选项数据HTML代码如下
!DOCTYPE html
html
head
meta charsetutf-8
title菜鸟教程(runoob.com)/title
/head
body
form action/cgi-bin/checkbox.py methodPOST target_blank
input typecheckbox namerunoob valueon / 菜鸟教程
input typecheckbox namegoogle valueon / Google
input typesubmit value选择站点 /
/form
/body
/html以下为 checkbox.py 文件的代码
#!/usr/bin/python3# 引入 CGI 处理模块
import cgi, cgitb# 创建 FieldStorage的实例
form cgi.FieldStorage()# 接收字段数据
if form.getvalue(google):google_flag 是
else:google_flag 否if form.getvalue(runoob):runoob_flag 是
else:runoob_flag 否print (Content-type:text/html)
print ()
print (html)
print (head)
print (meta charset\utf-8\)
print (title菜鸟教程 CGI 测试实例/title)
print (/head)
print (body)
print (h2 菜鸟教程是否选择了 : %s/h2 % runoob_flag)
print (h2 Google 是否选择了 : %s/h2 % google_flag)
print (/body)
print (/html)修改 checkbox.py 权限
chmod 755 checkbox.py浏览器访问 Gif 演示图 通过CGI程序传递Radio数据
Radio 只向服务器传递一个数据HTML代码如下
!DOCTYPE html
html
head
meta charsetutf-8
title菜鸟教程(runoob.com)/title
/head
body
form action/cgi-bin/radiobutton.py methodpost target_blank
input typeradio namesite valuerunoob / 菜鸟教程
input typeradio namesite valuegoogle / Google
input typesubmit value提交 /
/form
/body
/htmlradiobutton.py 脚本代码如下
#!/usr/bin/python3# 引入 CGI 处理模块
import cgi, cgitb# 创建 FieldStorage的实例
form cgi.FieldStorage()# 接收字段数据
if form.getvalue(site):site form.getvalue(site)
else:site 提交数据为空print (Content-type:text/html)
print ()
print (html)
print (head)
print (meta charset\utf-8\)
print (title菜鸟教程 CGI 测试实例/title)
print (/head)
print (body)
print (h2 选中的网站是 %s/h2 % site)
print (/body)
print (/html)修改 radiobutton.py 权限
chmod 755 radiobutton.py浏览器访问 Gif 演示图 通过CGI程序传递 Textarea 数据
Textarea 向服务器传递多行数据HTML 代码如下
!DOCTYPE html
html
head
meta charsetutf-8
title菜鸟教程(runoob.com)/title
/head
body
form action/cgi-bin/textarea.py methodpost target_blank
textarea nametextcontent cols40 rows4
在这里输入内容...
/textarea
input typesubmit value提交 /
/form
/body
/htmltextarea.py 脚本代码如下
#!/usr/bin/python3# 引入 CGI 处理模块
import cgi, cgitb# 创建 FieldStorage的实例
form cgi.FieldStorage()# 接收字段数据
if form.getvalue(textcontent):text_content form.getvalue(textcontent)
else:text_content 没有内容print (Content-type:text/html)
print ()
print (html)
print (head)
print (meta charset\utf-8\)
print (title菜鸟教程 CGI 测试实例/title)
print (/head)
print (body)
print (h2 输入的内容是%s/h2 % text_content)
print (/body)
print (/html)修改 textarea.py 权限 chmod 755 textarea.py浏览器访问 Gif 演示图 通过CGI程序传递下拉数据。
HTML 下拉框代码如下
!DOCTYPE html
html
head
meta charsetutf-8
title菜鸟教程(runoob.com)/title
/head
body
form action/cgi-bin/dropdown.py methodpost target_blank
select namedropdown
option valuerunoob selected菜鸟教程/option
option valuegoogleGoogle/option
/select
input typesubmit value提交/
/form
/body
/htmldropdown.py 脚本代码如下所示
#!/usr/bin/python3# 引入 CGI 处理模块
import cgi, cgitb# 创建 FieldStorage的实例
form cgi.FieldStorage()# 接收字段数据
if form.getvalue(dropdown):dropdown_value form.getvalue(dropdown)
else:dropdown_value 没有内容print (Content-type:text/html)
print ()
print (html)
print (head)
print (meta charset\utf-8\)
print (title菜鸟教程 CGI 测试实例/title)
print (/head)
print (body)
print (h2 选中的选项是%s/h2 % dropdown_value)
print (/body)
print (/html)修改 dropdown.py 权限
chmod 755 dropdown.py浏览器访问 Gif 演示图