新聞中心
Get和post是表單提交數(shù)據(jù)的兩種基本方式,get請求數(shù)據(jù)通過域名后綴url傳送,用戶可見,不安全,post請求數(shù)據(jù)通過在請求報文正文里傳輸,相對比較安全。

成都創(chuàng)新互聯(lián)是一家專業(yè)提供敘州企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站制作、網(wǎng)站設(shè)計、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為敘州眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進行中。
使用GET方法傳輸數(shù)據(jù)
GET方法發(fā)送編碼后的用戶信息到服務(wù)端,數(shù)據(jù)信息包含在請求頁面的URL上,以”?”號分割, 如下所示:
http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2
有關(guān) GET 請求的其他一些注釋: GET 請求可被緩存 GET 請求保留在瀏覽器歷史記錄中 GET 請求可被收藏為書簽 GET 請求不應(yīng)在處理敏感數(shù)據(jù)時使用 GET 請求有長度限制 GET 請求只應(yīng)當(dāng)用于取回數(shù)據(jù)
簡單的url實例:GET方法
以下是一個簡單的URL,使用GET方法向hello_get.py程序發(fā)送兩個參數(shù):
/cgi-bin/test.py?name=菜鳥教程&url=http://www.runoob.com
以下為hello_get.py文件的代碼:
#!/usr/bin/python3
# CGI處理模塊
import cgi, cgitb
# 創(chuàng)建 FieldStorage 的實例化
form = cgi.FieldStorage()
# 獲取數(shù)據(jù)
site_name = form.getvalue('name')
site_url = form.getvalue('url')
print ("Content-type:text/html")
print ()
print ("")
print ("")
print ("")
print ("")
print ("")
print ("")
print ("")
文件保存后修改 hello_get.py,修改文件權(quán)限為 755:
chmod 755 hello_get.py
瀏覽器請求輸出結(jié)果:
簡單的表單實例:GET方法
以下是一個通過HTML的表單使用GET方法向服務(wù)器發(fā)送兩個數(shù)據(jù),提交的服務(wù)器腳本同樣是hello_get.py文件,hello_get.html 代碼如下:
nbsp;html>
"utf-8">
菜鳥教程(runoob.com)
"/cgi-bin/hello_get.py" method=
"get"> 站點名稱:
type=
"text" name=
"name">
站點 URL:
type=
"text" name=
"url" />
type=
"submit" value=
"提交" />
默認(rèn)情況下 cgi-bin 目錄只能存放腳本文件,我們將 hello_get.html 存儲在 test 目錄下,修改文件權(quán)限為 755:
chmod 755 hello_get.html
Gif 演示如下所示:
使用POST方法傳遞數(shù)據(jù)
使用POST方法向服務(wù)器傳遞數(shù)據(jù)是更安全可靠的,像一些敏感信息如用戶密碼等需要使用POST傳輸數(shù)據(jù)。
以下同樣是hello_get.py ,它也可以處理瀏覽器提交的POST表單數(shù)據(jù):
#!/usr/bin/python3
# CGI處理模塊
import cgi, cgitb
# 創(chuàng)建 FieldStorage 的實例化
form = cgi.FieldStorage()
# 獲取數(shù)據(jù)
site_name = form.getvalue('name')
site_url = form.getvalue('url')
print ("Content-type:text/html")
print ()
print ("")
print ("")
print ("")
print ("")
print ("")
print ("")
print ("")
以下為表單通過POST方法(method=”post”)向服務(wù)器腳本 hello_get.py 提交數(shù)據(jù):
nbsp;html>
"utf-8">
菜鳥教程(runoob.com)
"/cgi-bin/hello_get.py" method=
"post"> 站點名稱:
type=
"text" name=
"name">
站點 URL:
type=
"text" name=
"url" />
type=
"submit" value=
"提交" />
Gif 演示如下所示:
本文題目:詳解Python中Get和post請求
新聞來源:http://fisionsoft.com.cn/article/djsjesp.html


咨詢
建站咨詢
