httplib2功能介紹:http://code.google.com/p/httplib2/
httplib2實例頁面:http://code.google.com/p/httplib2/w/list
httplib2問題提交:http://code.google.com/p/httplib2/issues/list
好吧,我覺得官方的樣例還是比較全的,這里就直接貼一下吧。
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/Meet-Ares", "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
- #!/usr/bin/env python
- 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=4
- h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, 'localhost', 8000))
- r,c = h.request("http://bitworking.org/news/")
======================================================================================
下面是我自己對模塊功能的嘗試:
- 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對象
- 其實就是一個包含所有頭信息的字典,因為它本身就是集成自字典對象的
另外,httplib2模塊本身還有其它的對象或屬性,可以通過print dir(httplib2)來查看