httplib2库访训http和https等的使用
import httplib2
#首先我们要访问的是https是用web但是没有进行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