新聞中心
GET和POST是HTTP請(qǐng)求的兩種基本方法,本篇文章重點(diǎn)就為大家講解一下GET和POST方法。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、東山網(wǎng)絡(luò)推廣、小程序制作、東山網(wǎng)絡(luò)營(yíng)銷、東山企業(yè)策劃、東山品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供東山建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
使用GET方法傳輸數(shù)據(jù)
GET方法發(fā)送編碼后的用戶信息到服務(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)求的其他一些注釋: GET 請(qǐng)求可被緩存 GET 請(qǐng)求保留在瀏覽器歷史記錄中 GET 請(qǐng)求可被收藏為書簽 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=菜鳥教程&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">
菜鳥教程(runoob.com)
"/cgi-bin/hello_get.py" method=
"get"> 站點(diǎn)名稱:
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ù)是更安全可靠的,像一些敏感信息如用戶密碼等需要使用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")
prinprint ("")
print ("")
print ("")
print ("")
print ("")
print ("")
print ("")
以下為表單通過(guò)POST方法(method=”post”)向服務(wù)器腳本 hello_get.py 提交數(shù)據(jù):
nbsp;html>
"utf-8">
菜鳥教程(runoob.com)
"/cgi-bin/hello_get.py" method=
"post"> 站點(diǎn)名稱:
type=
"text" name=
"name">
站點(diǎn) URL:
type=
"text" name=
"url" />
type=
"submit" value=
"提交" />
Gif 演示如下所示:
分享題目:詳解Python中的GET和POST方法
文章來(lái)源:http://fisionsoft.com.cn/article/coghejs.html


咨詢
建站咨詢
