使用python实现get方法和post方法传值,多选按钮,单选按钮、文本编辑区、下拉列表数据的传递,cookie的设置文件上传,文件下载。本文未经整理,仅供参考
#!/usr/bin/python# -*- coding: UTF-8 -*-print "Content-type:text/html"print # 空行,告诉服务器结束头部print ''print ''print ' 'print 'Hello Word - 我的第一个 CGI 程序!'print ''print ''print 'Hello Word! 我是来自菜鸟教程的第一CGI程序
'print ''print ''开启httpd服务器后,可以直接把该文件放到网站目录下,浏览器访问会正常显示该页面//网页传值//test.html代码 菜鸟教程(runoob.com)
//hello_get.py代码#!/usr/bin/python# -*- coding: UTF-8 -*-# filename:test.py# CGI处理模块import cgi, cgitb # 创建 FieldStorage 的实例化form = cgi.FieldStorage() # 获取数据site_name = form.getvalue('name')site_url = form.getvalue('url')print "Content-type:text/html"printprint ""print ""print "
"print "
菜鸟教程 CGI 测试实例"print ""print ""print "
%s官网:%s
" % (site_name, site_url)print ""print ""//如果想把上面的get方法修改为post方法,只需要把get改为post,后一种方法更加安全,不会在网址那里显示出密码//传递checkbox数据
菜鸟教程(runoob.com) #!/usr/bin/python# -*- coding: UTF-8 -*-# 引入 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"printprint ""print ""print "
"print "
菜鸟教程 CGI 测试实例"print ""print ""print "
菜鸟教程是否选择了 : %s
" % runoob_flagprint "
Google 是否选择了 : %s
" % google_flagprint ""print ""//传递radio数据
菜鸟教程(runoob.com) #!/usr/bin/python# -*- coding: UTF-8 -*-# 引入 CGI 处理模块 import cgi, cgitb # 创建 FieldStorage的实例 form = cgi.FieldStorage() # 接收字段数据if form.getvalue('site'): site = form.getvalue('site')else: site = "提交数据为空"print "Content-type:text/html"printprint ""print ""print "
"print "
菜鸟教程 CGI 测试实例"print ""print ""print "
选中的网站是 %s
" % siteprint ""print ""//传递Textarea数据
菜鸟教程(runoob.com) #!/usr/bin/python# -*- coding: UTF-8 -*-# 引入 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"printprint ""print "";print "
"print "
菜鸟教程 CGI 测试实例"print ""print ""print "
输入的内容是:%s
" % text_contentprint ""print ""//传递下拉数据
菜鸟教程(runoob.com) #!/usr/bin/python# -*- coding: UTF-8 -*-# 引入 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"printprint ""print ""print "
"print "
菜鸟教程 CGI 测试实例"print ""print ""print "
选中的选项是:%s
" % dropdown_valueprint ""print ""//cookie的设置#!/usr/bin/python# -*- coding: UTF-8 -*-# print 'Content-Type: text/html'print 'Set-Cookie: name="菜鸟教程";expires=Wed, 28 Aug 2016 18:30:00 GMT'printprint """
菜鸟教程(runoob.com) Cookie set OK!
"""#!/usr/bin/python# -*- coding: UTF-8 -*-# 导入模块import osimport Cookieprint "Content-type: text/html"printprint """
菜鸟教程(runoob.com)读取cookie信息
"""if 'HTTP_COOKIE' in os.environ: cookie_string=os.environ.get('HTTP_COOKIE') c=Cookie.SimpleCookie() c.load(cookie_string) try: data=c['name'].value print "cookie data: "+data+"
" except KeyError: print "cookie 没有设置或者已过去
"print """"""//文件上传
菜鸟教程(runoob.com) #!/usr/bin/python# -*- coding: UTF-8 -*-import cgi, osimport cgitb; cgitb.enable()form = cgi.FieldStorage()# 获取文件名fileitem = form['filename']# 检测文件是否上传if fileitem.filename: # 设置文件路径 fn = os.path.basename(fileitem.filename) //linux和unix系统需要把这句修改为fn = os.path.basename(fileitem.filename.replace("\\", "/" )) open('/tmp/' + fn, 'wb').write(fileitem.file.read()) message = '文件 "' + fn + '" 上传成功'else: message = '文件没有上传'print """\Content-Type: text/html\n
菜鸟教程(runoob.com) %s
""" % (message,)//文件下载#!/usr/bin/python# -*- coding: UTF-8 -*-# HTTP 头部print "Content-Disposition: attachment; filename=\"foo.txt\"";print# 打开文件fo = open("foo.txt", "rb")str = fo.read();print str# 关闭文件fo.close()