#!/usr/bin/env python2
import urllib,os
import sys
import time

import gzip
import cStringIO
def uncompressBuf(buf):
    zbuf = cStringIO.StringIO()
    zbuf.write(buf)
    zbuf.seek(0,0)
    zfile = gzip.GzipFile(mode = 'rb',  fileobj = zbuf)
    data=zfile.read()
    zfile.close()
    return data

def testurl(url):
   web=urllib.FancyURLopener()
   web.addheader("Accept-Encoding","gzip, deflate")
   web.addheader("Cache-Control","max-age=0")
   web.addheader("Pragma","no-cache")
   starttime=time.time()
   try:
      webpage = web.open(url)
      print "%s" % (url)
      for k,v in webpage.headers.items():  print "   ",k,"->",v
      data = webpage.read()
      datalen= len(data)
      runtime=time.time()-starttime
      print "   %10.0f b in %10.2f secs (%10.2f b/s)" % (datalen,runtime,datalen/(runtime))
      if webpage.headers.has_key('content-encoding') and webpage.headers['content-encoding']=='gzip':
          data= uncompressBuf(data)
          datalen= len(data)
          print "   %10.0f b in %10.2f secs (%10.2f b/s) effective due to gzipped page" % (datalen,runtime,datalen/(runtime))
      
   except IOError:
      print url, "is unreachable"
   

for u in sys.argv[1:]:
   testurl(u)

