httplib2---python下的http請求終結者(轉)html
httplib2功能介紹:http://code.google.com/p/httplib2/python
httplib2實例頁面:http://code.google.com/p/httplib2/w/listweb
httplib2問題提交:http://code.google.com/p/httplib2/issues/list緩存
好吧,我以爲官方的樣例仍是比較全的,這裏就直接貼一下吧。服務器
Simple Retrievalcookie
import httplib2 app
h = httplib2.Http(".cache") dom
resp, content = h.request("http://example.org/", "GET") socket
Authenticationgoogle
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對象
其實就是一個包含全部頭信息的字典,由於它自己就是集成自字典對象的