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

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


咨詢(xún)
建站咨詢(xún)
