home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.4)
-
- from urllib import *
- from urllib2 import *
- from gzip import GzipFile
- from StringIO import StringIO
- from __init__ import version
- import pprint
- DEBUG = 0
-
- class HTTPContentEncodingHandler(HTTPHandler):
- '''Inherit and add gzip/deflate/etc support to HTTP gets.'''
-
- def http_open(self, req):
- req.add_header('Accept-Encoding', 'gzip')
- req.add_header('User-Agent', 'BitTorrent/' + version)
- if DEBUG:
- print 'Sending:'
- print req.headers
- print '\n'
-
- fp = HTTPHandler.http_open(self, req)
- headers = fp.headers
- if DEBUG:
- pprint.pprint(headers.dict)
-
- url = fp.url
- resp = addinfourldecompress(fp, headers, url)
- if 'code' in dir(fp):
- resp.code = fp.code
-
- if 'msg' in dir(fp):
- resp.msg = fp.msg
-
- return resp
-
-
-
- class addinfourldecompress(addinfourl):
- '''Do gzip decompression if necessary. Do addinfourl stuff too.'''
-
- def __init__(self, fp, headers, url):
- if headers.has_key('content-encoding') and headers['content-encoding'] == 'gzip':
- if DEBUG:
- print 'Contents of Content-encoding: ' + headers['Content-encoding'] + '\n'
-
- self.gzip = 1
- self.rawfp = fp
- fp = GzipStream(fp)
- else:
- self.gzip = 0
- return addinfourl.__init__(self, fp, headers, url)
-
-
- def close(self):
- self.fp.close()
- if self.gzip:
- self.rawfp.close()
-
-
-
- def iscompressed(self):
- return self.gzip
-
-
-
- class GzipStream(StringIO):
- """Magically decompress a file object.
-
- This is not the most efficient way to do this but GzipFile() wants
- to seek, etc, which won't work for a stream such as that from a socket.
- So we copy the whole shebang info a StringIO object, decompress that
- then let people access the decompressed output as a StringIO object.
-
- The disadvantage is memory use and the advantage is random access.
-
- Will mess with fixing this later.
- """
-
- def __init__(self, fp):
- self.fp = fp
- compressed = StringIO()
- r = fp.read()
- while r:
- compressed.write(r)
- r = fp.read()
- compressed.seek(0, 0)
- gz = GzipFile(fileobj = compressed)
- str = ''
- r = gz.read()
- while r:
- str += r
- r = gz.read()
- compressed.close()
- gz.close()
- StringIO.__init__(self, str)
- del str
-
-
- def close(self):
- self.fp.close()
- return StringIO.close(self)
-
-
-
- def test():
- '''Test this module.
-
- At the moment this is lame.
- '''
- print 'Running unit tests.\n'
-
- def printcomp(fp):
-
- try:
- if fp.iscompressed():
- print 'GET was compressed.\n'
- else:
- print 'GET was uncompressed.\n'
- except:
- print "no iscompressed function! this shouldn't happen"
-
-
- print 'Trying to GET a compressed document...\n'
- fp = urlopen('http://a.scarywater.net/hng/index.shtml')
- print fp.read()
- printcomp(fp)
- fp.close()
- print 'Trying to GET an unknown document...\n'
- fp = urlopen('http://www.otaku.org/')
- print fp.read()
- printcomp(fp)
- fp.close()
-
- install_opener(build_opener(HTTPContentEncodingHandler))
- if __name__ == '__main__':
- test()
-
-