Simple Retrieval
import httplib2 h = httplib2.Http(".cache") resp, content = h.request("http://example.org/","GET")
Authentication
import httplib2 h = httplib2.Http(".cache") h.add_credentials('name', 'password') resp, content = h.request("https://example.org/chap/2", ##ssl + base認證 "PUT", body="This is text", headers={'content-type':'text/plain'} )
Cache-Control
import httplib2 h = httplib2.Http(".cache") resp, content = h.request("http://bitworking.org/") #請求被緩存,下次還會用這個緩存而不去發送的請求,緩存生效時間有web配置決定 ... resp, content = h.request("http://bitworking.org/", headers={'cache-control':'no-cache'}) ##設置不用緩存,當次將不用緩存,而是直接發一個新的請求
Forms
>>> from httplib2 import Http >>> from urllib import urlencode >>> h = Http() >>> data = dict(name="Joe", comment="A test comment") >>> resp, content = h.request("http://bitworking.org/news/223/MeetAres", "POST", urlencode(data)) >>> resp {'status': '200', 'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding,User-Agent', 'server': 'Apache', 'connection': 'close', 'date': 'Tue, 31 Jul 2007 15:29:52 GMT', 'content-type': 'text/html'}
Cookies
import urllib import httplib2 http = httplib2.Http() url = 'http://www.example.com/login' body = {'USERNAME': 'foo', 'PASSWORD': 'bar'} headers = {'Content-type': 'application/x-www-form-urlencoded'} response, content = http.request(url, 'POST', headers=headers, body=urllib.urlencode(body)) headers = {'Cookie': response['set-cookie']} ###將獲得cookie設置到請求頭中,以備下次請求 url = 'http://www.example.com/home' response, content = http.request(url, 'GET', headers=headers) ##本次請求就不用帶用戶名,密碼了
Proxies
import httplib2 import socks httplib2.debuglevel=4h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'localhost', 8000)) r,c = h.request("
======================================================================================
下面是我自己對模塊功能的嘗試:
Http對象的構造方法:
__init__(self, cache=None, timeout=None, proxy_info=None, ca_certs=None, disable_ssl_certificate_validation=False)
proxy_info 的值是一個 ProxyInfo instance.
|
| 'cache':
存放cache的位置,要麼爲字符串,要麼爲支持文件緩存接口的對象
|
| timeout:
超時時間,默認時會取python對socket鏈接超時的值
|
| ca_certs:
一個用於ssl服務器認證用的包涵了主CA認證的文件路徑,默認會使用httplib2綁定的證書
|
| disable_ssl_certificate_validation:
確定是否進行ssl認證
|
| add_certificate(self, key, cert, domain)
| 添加一個ssl認證key和文件
|
| add_credentials(self, name, password, domain='')
| 添加一個用戶名,密碼信息
|
| clear_credentials(self)
| 刪除掉所有的用戶名,密碼信息,貌似還是可以存多個用戶名和密碼
Http.request(self, uri, method='GET', body=None, headers=None, redirections=5, connection_type=None)
說明:
執行單次的http請求
uri:
一個以'http' 或 'https'開頭的資源定位符字串,必須是一個絕對的地址
method:
支持所有的http請求方式。如: GET, POST, DELETE, etc..
body:
請求的附件數據,一個經過urllib.urlencode編碼的字符串
headers:
請求頭信息,一個字典對象
redirections:
最大的自動連續的重定向次數默認爲5
返回:
(response, content)元組,response是一個httplib2.Response對象,content就是包含網頁源碼的字符串
httplib2.Response對象
其實就是一個包含所有頭信息的字典,因爲它本身就是集成自字典對象的
===========================================================================================
import httplib2 #首先我們要訪問的是https.使用沒有進行https認證的Http(),初始化時就設置好關閉ssl證書認證,disable_ssl_certificate_validation=True; h = httplib2.Http(disable_ssl_certificate_validation=True) d,c = h.request('https://ebank.xxxxx.com/pweb/test.do?actionType=1') #header print(d) #content print(c) #訪問普通http頁面和上面一樣 h = httplib2.Http() d,c = h.request("http://www.xxxx.com/") #header print(d) #content print(c) #當然也可以進行ssl證書認證 #h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_SOCKS5, self.px_url, self.proxy_port)) #h.add_certificate(self.certificate.ikeyfile, self.certificate.certfile, self.url) #resp, content = h.request("https://"+self.url+":"+str(self.remote_port)+self.path+query) #帶.cache h2 = httplib2.Http('.cache') resp2,content2 = h2.request('http://www.baidu.com/') print resp2 print content2 #再".cache"目錄下找到剛纔訪問的相關內容文件”#www.baidu.com,,f03f5717616221de41881be555473a02“,是baidu.com的緩存文件,用記事本打開可知裏面帶偶內容和httprespond頭信息 #帶.cache和ssl的用戶名密碼認證,算是結合上面兩個吧 h3 = httplib2.Http(".cache") h3.add_credentials('name', 'password') resp3, content3 = h3.request("https://www.google.com", "GET",headers={'content-type':'text/plain'} ) print resp3 print content3