python通過get,post方式發(fā)送http請求和接收http響應(yīng)的方法
本文實(shí)例講述了python通過get,post方式發(fā)送http請求和接收http響應(yīng)的方法。分享給大家供大家參考。具體如下:
測試用CGI,名字為test.py,放在apache的cgi-bin目錄下:
#!/usr/bin/python import cgi def main(): print "Content-type: text/html\n" form = cgi.FieldStorage() if form.has_key("ServiceCode") and form["ServiceCode"].value != "": print "<h1> Hello",form["ServiceCode"].value,"</h1>" else: print "<h1> Error! Please enter first name.</h1>" main()
python發(fā)送post和get請求
get請求:
使用get方式時(shí),請求數(shù)據(jù)直接放在url中。
方法一、
import urllib import urllib2 url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa" req = urllib2.Request(url) print req res_data = urllib2.urlopen(req) res = res_data.read() print res
方法二、
import httplib url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa" conn = httplib.HTTPConnection("192.168.81.16") conn.request(method="GET",url=url) response = conn.getresponse() res= response.read() print res
post請求:
使用post方式時(shí),數(shù)據(jù)放在data或者body中,不能放在url中,放在url中將被忽略。
方法一、
import urllib import urllib2 test_data = {'ServiceCode':'aaaa','b':'bbbbb'} test_data_urlencode = urllib.urlencode(test_data) requrl = "http://192.168.81.16/cgi-bin/python_test/test.py" req = urllib2.Request(url = requrl,data =test_data_urlencode) print req res_data = urllib2.urlopen(req) res = res_data.read() print res
方法二、
import urllib import httplib test_data = {'ServiceCode':'aaaa','b':'bbbbb'} test_data_urlencode = urllib.urlencode(test_data) requrl = "http://192.168.81.16/cgi-bin/python_test/test.py" headerdata = {"Host":"192.168.81.16"} conn = httplib.HTTPConnection("192.168.81.16") conn.request(method="POST",url=requrl,body=test_data_urlencode,headers = headerdata) response = conn.getresponse() res= response.read() print res
對python中json的使用不清楚,所以臨時(shí)使用了urllib.urlencode(test_data)方法;
模塊urllib,urllib2,httplib的區(qū)別
httplib實(shí)現(xiàn)了http和https的客戶端協(xié)議,但是在python中,模塊urllib和urllib2對httplib進(jìn)行了更上層的封裝。
介紹下例子中用到的函數(shù):
1、HTTPConnection函數(shù)
httplib.HTTPConnection(host[,port[,stict[,timeout]]])
這個(gè)是構(gòu)造函數(shù),表示一次與服務(wù)器之間的交互,即請求/響應(yīng)
host 標(biāo)識(shí)服務(wù)器主機(jī)(服務(wù)器IP或域名)
port 默認(rèn)值是80
strict 模式是False,表示無法解析服務(wù)器返回的狀態(tài)行時(shí),是否拋出BadStatusLine異常
例如:
conn = httplib.HTTPConnection("192.168.81.16",80) 與服務(wù)器建立鏈接。
2、HTTPConnection.request(method,url[,body[,header]])函數(shù)
這個(gè)是向服務(wù)器發(fā)送請求
method 請求的方式,一般是post或者get,
例如:
method="POST"或method="Get"
url 請求的資源,請求的資源(頁面或者CGI,我們這里是CGI)
例如:
url="http://192.168.81.16/cgi-bin/python_test/test.py" 請求CGI
或者
url="http://192.168.81.16/python_test/test.html" 請求頁面
body 需要提交到服務(wù)器的數(shù)據(jù),可以用json,也可以用上面的格式,json需要調(diào)用json模塊
headers 請求的http頭headerdata = {"Host":"192.168.81.16"}
例如:
test_data = {'ServiceCode':'aaaa','b':'bbbbb'} test_data_urlencode = urllib.urlencode(test_data) requrl = "http://192.168.81.16/cgi-bin/python_test/test.py" headerdata = {"Host":"192.168.81.16"} conn = httplib.HTTPConnection("192.168.81.16",80) conn.request(method="POST",url=requrl,body=test_data_urlencode,headers = headerdata)
conn在使用完畢后,應(yīng)該關(guān)閉,conn.close()
3、HTTPConnection.getresponse()函數(shù)
這個(gè)是獲取http響應(yīng),返回的對象是HTTPResponse的實(shí)例。
4、HTTPResponse介紹:
HTTPResponse的屬性如下:
read([amt]) 獲取響應(yīng)消息體,amt表示從響應(yīng)流中讀取指定字節(jié)的數(shù)據(jù),沒有指定時(shí),將全部數(shù)據(jù)讀出;
getheader(name[,default]) 獲得響應(yīng)的header,name是表示頭域名,在沒有頭域名的時(shí)候,default用來指定返回值
getheaders() 以列表的形式獲得header
例如:
date=response.getheader('date'); print date resheader='' resheader=response.getheaders(); print resheader
列形式的響應(yīng)頭部信息:
[('content-length', '295'), ('accept-ranges', 'bytes'), ('server', 'Apache'), ('last-modified', 'Sat, 31 Mar 2012 10:07:02 GMT'), ('connection', 'close'), ('etag', '"e8744-127-4bc871e4fdd80"'), ('date', 'Mon, 03 Sep 2012 10:01:47 GMT'), ('content-type', 'text/html')] date=response.getheader('date'); print date
取出響應(yīng)頭部的date的值。
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python 文本滾動(dòng)播放器的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python 文本滾動(dòng)播放器的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Python 實(shí)現(xiàn)加密過的PDF文件轉(zhuǎn)WORD格式
這篇文章主要介紹了Python 實(shí)現(xiàn)加密過的PDF文件轉(zhuǎn)WORD格式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02Python數(shù)學(xué)建模StatsModels統(tǒng)計(jì)回歸模型數(shù)據(jù)的準(zhǔn)備
這篇文章主要介紹了Python數(shù)學(xué)建模StatsModels統(tǒng)計(jì)回歸模型數(shù)據(jù)的準(zhǔn)備學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10selenium?drag_and_drop不生效的解決辦法
本文主要介紹了selenium?drag_and_drop不生效的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python用HBuilder創(chuàng)建交流社區(qū)APP
這篇文章主要講解Python使用HBuilder創(chuàng)建交流社區(qū)APP,使用HBuilder做一個(gè)簡單的社區(qū)瀏覽界面,下面文章附有詳細(xì)的代碼,需要的朋友可以參考一下2021-11-11Python實(shí)現(xiàn)企業(yè)微信通知機(jī)器人的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)對企業(yè)微信進(jìn)行群通知的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02