home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / zurllib.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  4.2 KB  |  140 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. from urllib import *
  5. from urllib2 import *
  6. from gzip import GzipFile
  7. from StringIO import StringIO
  8. from __init__ import version
  9. import pprint
  10. DEBUG = 0
  11.  
  12. class HTTPContentEncodingHandler(HTTPHandler):
  13.     '''Inherit and add gzip/deflate/etc support to HTTP gets.'''
  14.     
  15.     def http_open(self, req):
  16.         req.add_header('Accept-Encoding', 'gzip')
  17.         req.add_header('User-Agent', 'BitTorrent/' + version)
  18.         if DEBUG:
  19.             print 'Sending:'
  20.             print req.headers
  21.             print '\n'
  22.         
  23.         fp = HTTPHandler.http_open(self, req)
  24.         headers = fp.headers
  25.         if DEBUG:
  26.             pprint.pprint(headers.dict)
  27.         
  28.         url = fp.url
  29.         resp = addinfourldecompress(fp, headers, url)
  30.         if 'code' in dir(fp):
  31.             resp.code = fp.code
  32.         
  33.         if 'msg' in dir(fp):
  34.             resp.msg = fp.msg
  35.         
  36.         return resp
  37.  
  38.  
  39.  
  40. class addinfourldecompress(addinfourl):
  41.     '''Do gzip decompression if necessary. Do addinfourl stuff too.'''
  42.     
  43.     def __init__(self, fp, headers, url):
  44.         if headers.has_key('content-encoding') and headers['content-encoding'] == 'gzip':
  45.             if DEBUG:
  46.                 print 'Contents of Content-encoding: ' + headers['Content-encoding'] + '\n'
  47.             
  48.             self.gzip = 1
  49.             self.rawfp = fp
  50.             fp = GzipStream(fp)
  51.         else:
  52.             self.gzip = 0
  53.         return addinfourl.__init__(self, fp, headers, url)
  54.  
  55.     
  56.     def close(self):
  57.         self.fp.close()
  58.         if self.gzip:
  59.             self.rawfp.close()
  60.         
  61.  
  62.     
  63.     def iscompressed(self):
  64.         return self.gzip
  65.  
  66.  
  67.  
  68. class GzipStream(StringIO):
  69.     """Magically decompress a file object.
  70.  
  71.        This is not the most efficient way to do this but GzipFile() wants
  72.        to seek, etc, which won't work for a stream such as that from a socket.
  73.        So we copy the whole shebang info a StringIO object, decompress that
  74.        then let people access the decompressed output as a StringIO object.
  75.  
  76.        The disadvantage is memory use and the advantage is random access.
  77.  
  78.        Will mess with fixing this later.
  79.     """
  80.     
  81.     def __init__(self, fp):
  82.         self.fp = fp
  83.         compressed = StringIO()
  84.         r = fp.read()
  85.         while r:
  86.             compressed.write(r)
  87.             r = fp.read()
  88.         compressed.seek(0, 0)
  89.         gz = GzipFile(fileobj = compressed)
  90.         str = ''
  91.         r = gz.read()
  92.         while r:
  93.             str += r
  94.             r = gz.read()
  95.         compressed.close()
  96.         gz.close()
  97.         StringIO.__init__(self, str)
  98.         del str
  99.  
  100.     
  101.     def close(self):
  102.         self.fp.close()
  103.         return StringIO.close(self)
  104.  
  105.  
  106.  
  107. def test():
  108.     '''Test this module.
  109.  
  110.        At the moment this is lame.
  111.     '''
  112.     print 'Running unit tests.\n'
  113.     
  114.     def printcomp(fp):
  115.         
  116.         try:
  117.             if fp.iscompressed():
  118.                 print 'GET was compressed.\n'
  119.             else:
  120.                 print 'GET was uncompressed.\n'
  121.         except:
  122.             print "no iscompressed function!  this shouldn't happen"
  123.  
  124.  
  125.     print 'Trying to GET a compressed document...\n'
  126.     fp = urlopen('http://a.scarywater.net/hng/index.shtml')
  127.     print fp.read()
  128.     printcomp(fp)
  129.     fp.close()
  130.     print 'Trying to GET an unknown document...\n'
  131.     fp = urlopen('http://www.otaku.org/')
  132.     print fp.read()
  133.     printcomp(fp)
  134.     fp.close()
  135.  
  136. install_opener(build_opener(HTTPContentEncodingHandler))
  137. if __name__ == '__main__':
  138.     test()
  139.  
  140.