|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | +import urllib,urllib2,cookielib,zlib |
| 4 | +from gzip import GzipFile |
| 5 | +from StringIO import StringIO |
| 6 | +import sys |
| 7 | + |
| 8 | +class HttpWrapperException(Exception): |
| 9 | + """ |
| 10 | + Custom HttpWrapper Exception |
| 11 | + """ |
| 12 | + pass |
| 13 | + |
| 14 | +class HttpWrapper: |
| 15 | + """ |
| 16 | + A wrapper of http Request, integrated with cookie handler and smart referer handler |
| 17 | +
|
| 18 | + Usage: |
| 19 | + HttpWrapper('http://xxx.com',data=dict) |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, url, postData=None,enableAutoRedirect = True,enableProxy = False,proxyDict = None,**header): |
| 23 | + """ |
| 24 | + url : url you want to request |
| 25 | + postData : data you want to post to the server, must be dict type,like {data1 : 'data'} |
| 26 | + enableAutoRedirect : auto redirect when server return 301,302 error |
| 27 | + enableProxy : whether enable proxy |
| 28 | + ProxyDict : proxy info. e.g ProxyDict = {'http':'10.182.45.231:80','https':'10.182.45.231:80'} |
| 29 | + **header : other request info. e.g. referer='www.sina.com.cn' |
| 30 | + """ |
| 31 | + |
| 32 | + self.url = url |
| 33 | + self.postData = postData |
| 34 | + self.enableAutoRedirect = enableAutoRedirect |
| 35 | + self.enableProxy = enableProxy |
| 36 | + self.ProxyDict = proxyDict |
| 37 | + #tell server where i'm from,some explain about referer http://www.fwolf.com/blog/post/320 |
| 38 | + if 'referer' in header: |
| 39 | + self.referer = header['referer'] |
| 40 | + else: |
| 41 | + self.referer = None |
| 42 | + |
| 43 | + if 'user-agent' in header: |
| 44 | + self.user_agent = header['user-agent'] |
| 45 | + else: |
| 46 | + self.user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16' |
| 47 | + |
| 48 | + self.__SetupHandler() |
| 49 | + self.__SendRequest(self.__SetupRequest()) |
| 50 | + |
| 51 | + class ContentEncodingProcessor(urllib2.BaseHandler): |
| 52 | + """ |
| 53 | + A handler to add gzip capabilities to urllib2 requests |
| 54 | + """ |
| 55 | + |
| 56 | + # add headers to requests |
| 57 | + def http_request(self, req): |
| 58 | + req.add_header("Accept-Encoding", "gzip,deflate") |
| 59 | + return req |
| 60 | + |
| 61 | + # decode |
| 62 | + def http_response(self, req, resp): |
| 63 | + old_resp = resp |
| 64 | + # gzip |
| 65 | + if resp.headers.get("content-encoding") == "gzip": |
| 66 | + gz = GzipFile( fileobj = StringIO(resp.read()), mode="r") |
| 67 | + resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) |
| 68 | + resp.msg = old_resp.msg |
| 69 | + # deflate |
| 70 | + if resp.headers.get("content-encoding") == "deflate": |
| 71 | + gz = StringIO(self.deflate(resp.read()) ) |
| 72 | + resp = urllib2.addinfourl(gz, old_resp.headers, old_resp.url, old_resp.code) # 'class to add info() and |
| 73 | + resp.msg = old_resp.msg |
| 74 | + return resp |
| 75 | + |
| 76 | + def deflate(self,data): # zlib only provides the zlib compress format, not the deflate format; |
| 77 | + try: # so on top of all there's this workaround: |
| 78 | + return zlib.decompress(data, -zlib.MAX_WBITS) |
| 79 | + except zlib.error: |
| 80 | + return zlib.decompress(data) |
| 81 | + |
| 82 | + |
| 83 | + def __SetupHandler(self): |
| 84 | + """ |
| 85 | + setup cookie handler, proxy handler and redirect hander for urllib2 library |
| 86 | + """ |
| 87 | + cj = cookielib.CookieJar() |
| 88 | + opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) |
| 89 | + opener.addheaders = [('User-agent', self.user_agent)] |
| 90 | + if self.enableAutoRedirect == True: |
| 91 | + opener.add_handler(urllib2.HTTPRedirectHandler()) |
| 92 | + if self.enableProxy == True: |
| 93 | + if self.proxyDict == None: |
| 94 | + raise HttpWrapperException('you must specify proxyDict when enabled Proxy') |
| 95 | + opener.add_handler(urllib2.ProxyHandler(self.proxyDict)) |
| 96 | + urllib2.install_opener(opener) |
| 97 | + |
| 98 | + def __SetupRequest(self): |
| 99 | + """ |
| 100 | + setup request information |
| 101 | + """ |
| 102 | + if self.url is None or self.url == '': |
| 103 | + raise HttpWrapperException("url can't be empty!") |
| 104 | + |
| 105 | + if self.postData is not None: |
| 106 | + req = urllib2.Request(self.url, urllib.urlencode(self.postData)) |
| 107 | + else: |
| 108 | + req = urllib2.Request(self.url) |
| 109 | + |
| 110 | + if self.referer: |
| 111 | + req.add_header('referer', self.referer) |
| 112 | + |
| 113 | + if self.user_agent: |
| 114 | + req.add_header('user-agent', self.user_agent) |
| 115 | + |
| 116 | + return req |
| 117 | + |
| 118 | + def __SendRequest(self,req): |
| 119 | + try: |
| 120 | + res = urllib2.urlopen(req) |
| 121 | + self.source = res.read() |
| 122 | + self.code = res.getcode() |
| 123 | + #get real url, if we have 301 redirect page |
| 124 | + self.url = res.geturl() |
| 125 | + self.head_dict = res.info().dict |
| 126 | + res.close() |
| 127 | + except urllib2.HTTPError,e: |
| 128 | + self.code = e.code |
| 129 | + self.source = e.fp.read() |
| 130 | + self.head_dict = e.headers |
| 131 | + #print e.code |
| 132 | + #print e.msg |
| 133 | + #print e.headers |
| 134 | + #print e.fp.read() |
| 135 | + #print u"error happended:\r\n Location: HttpWrapper.__SendRequest \r\n Error Information:", sys.exc_info()[1] |
| 136 | + |
| 137 | + |
| 138 | + def GetResponseCode(self): |
| 139 | + """ |
| 140 | + get response code, e.g. 200 or 302 |
| 141 | + """ |
| 142 | + if self.code: |
| 143 | + return self.code |
| 144 | + return -1 |
| 145 | + |
| 146 | + def GetUrl(self): |
| 147 | + if self.url: |
| 148 | + return self.url |
| 149 | + return None |
| 150 | + |
| 151 | + def GetContent(self): |
| 152 | + """ |
| 153 | + get content body of the response. |
| 154 | + usually, you need to decode those content. for example, GetContent().decode('utf-8') |
| 155 | + """ |
| 156 | + if "source" in dir(self): |
| 157 | + return self.source |
| 158 | + else: |
| 159 | + raise HttpWrapperException(u'HttpWrapper error happended:\r\n Location: HttpWrapper.GetContent \r\n Error Information:no content find') |
| 160 | + |
| 161 | + def GetHeaderInfo(self): |
| 162 | + return self.head_dict |
| 163 | + |
| 164 | + def GetCookie(self): |
| 165 | + if 'set-cookie' in self.head_dict: |
| 166 | + return self.head_dict['set-cookie'] |
| 167 | + else: |
| 168 | + return None |
| 169 | + |
| 170 | + def GetContentType(self): |
| 171 | + if 'content-type' in self.head_dict: |
| 172 | + return self.head_dict['content-type'] |
| 173 | + else: |
| 174 | + return None |
| 175 | + |
| 176 | + def GetCharset(self): |
| 177 | + contentType = self.GetContentType() |
| 178 | + if contentType is not None: |
| 179 | + index = contentType.find("charset") |
| 180 | + if index > 0: |
| 181 | + return contentType[index+8:] |
| 182 | + return None |
| 183 | + |
| 184 | + def GetExpiresTime(self): |
| 185 | + if 'expires' in self.head_dict: |
| 186 | + return self.head_dict['expires'] |
| 187 | + else: |
| 188 | + return None |
| 189 | + |
| 190 | + def GetServerName(self): |
| 191 | + if 'server' in self.head_dict: |
| 192 | + return self.head_dict['server'] |
| 193 | + else: |
| 194 | + return None |
| 195 | + |
| 196 | +if __name__ == '__main__': |
| 197 | + b = HttpWrapper('http://www.sina.com/test/fe') |
| 198 | + print b.GetContent().decode('utf-8') |
0 commit comments