Package httxlib :: Module httxcompression
[hide private]
[frames] | no frames]

Source Code for Module httxlib.httxcompression

 1  #!/usr/bin/env python 
 2  # -*- coding: latin-1; py-indent-offset:4 -*- 
 3  ################################################################################ 
 4  #  
 5  # This file is part of HttxLib 
 6  # 
 7  # HttxLib is an HTTP(s) Python library suited multithreaded/multidomain 
 8  # applications 
 9  # 
10  # Copyright (C) 2010-2011 Daniel Rodriguez (aka Daniel Rodriksson) 
11  # Copyright (C) 2011 Sensible Odds Ltd 
12  # 
13  # You can learn more and contact the author at: 
14  # 
15  #    http://code.google.com/p/httxlib/ 
16  # 
17  # HttxLib is free software: you can redistribute it and/or modify 
18  # it under the terms of the GNU General Public License as published by 
19  # the Free Software Foundation, either version 3 of the License, or 
20  # (at your option) any later version. 
21  # 
22  # HttxLib is distributed in the hope that it will be useful, 
23  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
24  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
25  # GNU General Public License for more details. 
26  # 
27  # You should have received a copy of the GNU General Public License 
28  # along with HttxLib. If not, see <http://www.gnu.org/licenses/>. 
29  # 
30  ################################################################################ 
31  ''' 
32  Utility module to perform decompression of an http body 
33  ''' 
34   
35  from bz2 import decompress as bz2decompress 
36  from gzip import GzipFile 
37  from zlib import decompress as zlibdecompress, error as zliberror, MAX_WBITS as zlibMAX_WBITS 
38   
39 -def httxdecompress(response):
40 ''' 41 Decompression of the body from response. 42 43 The supported compression types are gzip, bzip2 and deflate 44 45 @param response: the response from the http server containing the body 46 @type response: L{HttxResponse} 47 ''' 48 49 decompmethod = response.getheader('content-encoding') 50 51 if not decompmethod: 52 return 53 54 if decompmethod == 'gzip': 55 gzipper = GzipFile(fileobj=response.bodyfile) 56 response.body = gzipper.read() 57 58 elif decompmethod == 'deflate': 59 try: 60 response.body = zlibdecompress(response.body) 61 except zliberror: 62 # Many web sites fail to send the first bytes of the header 63 # possibly it is a header-stripped gzip file 64 response.body = zlibdecompress(response.body, -zlibMAX_WBITS) 65 66 elif decompmethod == 'bzip2': 67 response.body = bz2decompress(response.body)
68