欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python通過get,post方式發(fā)送http請求和接收http響應(yīng)的方法

 更新時(shí)間:2015年05月26日 16:39:53   作者:無影  
這篇文章主要介紹了python通過get,post方式發(fā)送http請求和接收http響應(yīng)的方法,涉及Python使用urllib模塊與urllib2模塊實(shí)現(xiàn)get與post發(fā)送數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下

本文實(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)文章

最新評論