httplib2---python下的http请求终结者(转)
2016-04-07 00:00
357 查看
httplib2---python下的http请求终结者(转)
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功能介绍: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对象
其实就是一个包含所有头信息的字典,因为它本身就是集成自字典对象的
相关文章推荐
- [10秒学会] - iOS 网络检测(如需要更详细 请用Reachability)
- Response.StatusCode的HTTP状态代码列表 【转】
- http状态码521——记一次网页爬取
- httpclient访问https网站失败(peer not authenticated)的解决办法
- BZOJ 3931 CQOI 2015 网络吞吐量 最大流 最短路
- VMWare的网络三种设置方式比较
- Tcp性能调优 解决Tcp长延时
- Meta http-equiv属性详解(转)
- Apache2.4中开通HTTP基本认证
- TCP/IP基础(四)
- linux网络配置命令之ifconfig、ip和route
- Ping测试网络连接状况
- 一次复杂而简单的网络诊断
- 牛客网每日打卡-网络基础-20160406
- [C#基础]网络编程(三):protobuf-net
- <meta>中的http-equiv的参数值
- https://github.com/oneuijs/You-Dont-Need-jQuery
- bzoj 1061: [Noi2008]志愿者招募(线性规划+网络流)
- hdu 5437 Alisha’s Party(长春网络赛——优先队列)
- HttpClient学习