python httplib2应用get post
import httplib2,time
#装饰器方法,用于记录方法消耗时间
#推荐将print 改成log
def timer(func):
def _warpper(self,*argv):
start = time.time()
result = func(self,*argv)
cost = time.time() - start
print 'The function %s coust time %f sec' % (func.func_name,cost)
return result
return _warpper
class Spider(object):
"""docstring for Spider"""
def __init__(self):
super(Spider, self).__init__()
self.h = httplib2.Http('.cache')
@timer
def httpGet(self,urlstr,word=""):
#httplib2.debuglevel = 1
urlstr = urlstr
self.head,self.content = self.h.request(urlstr)
#print(content)
@timer
def httpPost(self,urlstr,data):
from urllib import urlencode
self.head,self.content = self.h.request(urlstr, 'POST', urlencode(data), headers={'Content-Type': 'application/x-www-form-urlencoded'})
def getContent(self):
return self.content.decode('utf-8')
def getResponse(self):
return self.head
- #post传参数据
data = {'password':'s2105535','submit':'Login','username':'qq64397232'}
spider = Spider()
spider.httpPost(
'http:/12121212.com'
,data)
浮生潦草闲愁广,一听啤酒一口尽