国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python通過get,post方式發送http請求和接收http響應的方法

2019-11-25 17:22:11
字體:
來源:轉載
供稿:網友

本文實例講述了python通過get,post方式發送http請求和接收http響應的方法。分享給大家供大家參考。具體如下:

測試用CGI,名字為test.py,放在apache的cgi-bin目錄下:

#!/usr/bin/pythonimport cgidef 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發送post和get請求

get請求:

使用get方式時,請求數據直接放在url中。

方法一、

import urllibimport urllib2url = "http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa"req = urllib2.Request(url)print reqres_data = urllib2.urlopen(req)res = res_data.read()print res

方法二、

import httpliburl = "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方式時,數據放在data或者body中,不能放在url中,放在url中將被忽略。

方法一、

import urllibimport urllib2test_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 reqres_data = urllib2.urlopen(req)res = res_data.read()print res

方法二、

import urllibimport 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的使用不清楚,所以臨時使用了urllib.urlencode(test_data)方法;

模塊urllib,urllib2,httplib的區別

httplib實現了http和https的客戶端協議,但是在python中,模塊urllib和urllib2對httplib進行了更上層的封裝。

介紹下例子中用到的函數:

1、HTTPConnection函數

httplib.HTTPConnection(host[,port[,stict[,timeout]]])
這個是構造函數,表示一次與服務器之間的交互,即請求/響應
host 標識服務器主機(服務器IP或域名)
port 默認值是80
strict 模式是False,表示無法解析服務器返回的狀態行時,是否拋出BadStatusLine異常

例如:

conn = httplib.HTTPConnection("192.168.81.16",80) 與服務器建立鏈接。

2、HTTPConnection.request(method,url[,body[,header]])函數

這個是向服務器發送請求

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 需要提交到服務器的數據,可以用json,也可以用上面的格式,json需要調用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在使用完畢后,應該關閉,conn.close()

3、HTTPConnection.getresponse()函數

這個是獲取http響應,返回的對象是HTTPResponse的實例。

4、HTTPResponse介紹:

HTTPResponse的屬性如下:
read([amt]) 獲取響應消息體,amt表示從響應流中讀取指定字節的數據,沒有指定時,將全部數據讀出;
getheader(name[,default]) 獲得響應的header,name是表示頭域名,在沒有頭域名的時候,default用來指定返回值
getheaders() 以列表的形式獲得header

例如:

date=response.getheader('date');print dateresheader=''resheader=response.getheaders();print resheader

列形式的響應頭部信息:

[('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

取出響應頭部的date的值。

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴东县| 乌拉特前旗| 建水县| 绥化市| 博兴县| 碌曲县| 洛宁县| 禹城市| 宁晋县| 内黄县| 鞍山市| 贵港市| 六盘水市| 策勒县| 赞皇县| 天祝| 庆城县| 安陆市| 大庆市| 开江县| 宝应县| 黄平县| 怀远县| 桐梓县| 四子王旗| 汝阳县| 巴彦县| 罗田县| 东丰县| 苍南县| 漳州市| 星子县| 温州市| 关岭| 腾冲县| 乌兰察布市| 安陆市| 南雄市| 华池县| 昭觉县| 昭觉县|