#!/usr/bin/env python
#-*- coding:utf-8 -*-
#
# header_topla - Collects HTTP response headers, sorts and counts response items
# http://www.sertankolat.com/2011/12/http-baslik-analizi.html
#
# Copyright (c)2011  Sertan Kolat - sertan@olympos.org
#

# version 3

import urllib, urllib2, sys

if len(sys.argv) < 2:
	print 'Usage:\t./header_topla.py http://target 20'
	sys.exit(2)

class NoRedirectHandler(urllib2.HTTPRedirectHandler):
	#via stackoverflow.com
	def http_error_302(self, req, fp, code, msg, headers):
		infourl = urllib.addinfourl(fp, headers, req.get_full_url())
		infourl.status = code
		infourl.code = code
		return infourl
	http_error_300 = http_error_302
	http_error_301 = http_error_302
	http_error_303 = http_error_302
	http_error_307 = http_error_302

opener = urllib2.build_opener() 
# if you have problems with HTTP redirects, uncomment the line below
#opener = urllib2.build_opener(NoRedirectHandler())
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; ru)')]

mylist=[]

for i in range(0,int(sys.argv[2])):
	u = opener.open(sys.argv[1])
	mylist += u.headers.headers
	u.close()
	
for h in [(mylist.count(j), j.strip()) for j in sorted(set(mylist))]:
	print '\t%5s\t%s' % h

