使用python實現get方法和post方法傳值,多選按鈕,單選按鈕、文本編輯區、下拉列表數據的傳遞,cookie的設置文件上傳,文件下載。本文未經整理,僅供參考html
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print "Content-type:text/html"
print # 空行,告訴服務器結束頭部
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>Hello Word - 個人第一個 CGI 程序!</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! 我是來自菜鳥教程的第一CGI程序</h2>'
print '</body>'
print '</html>'
開啓httpd服務器後,能夠直接把該文件放到網站目錄下,瀏覽器訪問會正常顯示該頁面
//網頁傳值
//test.html代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<form action="/cgi-bin/hello_get.py" method="get">
站點名稱: <input type="text" name="name"> <br />
站點 URL: <input type="text" name="url" />
<input type="submit" value="提交" />
</form>
</body>
</html>
//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"
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>"
//若是想把上面的get方法修改成post方法,只須要把get改成post,後一種方法更加安全,不會在網址那裏顯示出密碼
//傳遞checkbox數據
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<form action="/cgi-bin/checkbox.py" method="POST" target="_blank">
<input type="checkbox" name="runoob" value="on" /> 菜鳥教程
<input type="checkbox" name="google" value="on" /> Google
<input type="submit" value="選擇站點" />
</form>
</body>
</html>
#!/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"
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>"
//傳遞radio數據
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<form action="/cgi-bin/radiobutton.py" method="post" target="_blank">
<input type="radio" name="site" value="runoob" /> 菜鳥教程
<input type="radio" name="site" value="google" /> Google
<input type="submit" value="提交" />
</form>
</body>
</html>
#!/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"
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>"
//傳遞Textarea數據
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<form action="/cgi-bin/textarea.py" method="post" target="_blank">
<textarea name="textcontent" cols="40" rows="4">
在這裏輸入內容...
</textarea>
<input type="submit" value="提交" />
</form>
</body>
</html>
#!/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"
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>"
//傳遞下拉數據
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<form action="/cgi-bin/dropdown.py" method="post" target="_blank">
<select name="dropdown">
<option value="runoob" selected>菜鳥教程</option>
<option value="google">Google</option>
</select>
<input type="submit" value="提交"/>
</form>
</body>
</html>
#!/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"
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>"
//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'
print
print """
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<h1>Cookie set OK!</h1>
</body>
</html>
"""
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 導入模塊
import os
import Cookie
print "Content-type: text/html"
print
print """
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<h1>讀取cookie信息</h1>
"""
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+"<br>"
except KeyError:
print "cookie 沒有設置或者已過去<br>"
print """
</body>
</html>
"""
//文件上傳
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<form enctype="multipart/form-data" action="/cgi-bin/save_file.py" method="post">
<p>選中文件: <input type="file" name="filename" /></p>
<p><input type="submit" value="上傳" /></p>
</form>
</body>
</html>
#!/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) //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
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
</head>
<body>
<p>%s</p>
</body>
</html>
""" % (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()