Skip to content

Instantly share code, notes, and snippets.

@sri
Created October 11, 2010 05:37
Show Gist options
  • Save sri/620035 to your computer and use it in GitHub Desktop.
Save sri/620035 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
# USAGE:
# h -- prints out the http server headers for a given URL
# Command-Line: h [agent] URL
# Agent can be any of: ff, chrome, ie, googlebot, safari
# If nothing is given on the command line, clipboard data
# is tried (under OS X and Windows). If it contains a valid
# URL, then that is used.
import sys
import http.client
import urllib.parse
import textwrap
import subprocess
AGENTS = {
'ff': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2b4) ' \
'Gecko/20091124 Firefox/3.6b4',
'chrome': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 ' \
'(KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4',
'ie': 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SLCC1; .NET ' \
'CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322)',
'googlebot': 'Googlebot/2.1 (+http://www.googlebot.com/bot.html)',
'safari': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/534.1+ ' \
'(KHTML, like Gecko) Version/5.0 Safari/533.16'
}
def wrap(s, indent_by):
prefix = ' ' * indent_by
return '\n'.join(prefix + x for x in textwrap.wrap(s, 50)).lstrip()
def url_from_clipboard():
data = ''
if sys.platform == 'darwin':
data = subprocess.getoutput('pbpaste')
elif sys.platform == 'win32':
try:
import ctypes
except ImportError:
pass
else:
ctypes.windll.user32.OpenClipboard(None)
pc = ctypes.windll.user32.GetClipboardData(1)
data = ctypes.c_char_p(pc).value
if data.startswith('http') or data.startswith('www.'):
return data
else:
return None
def get_headers(url, user_agent):
if not url.startswith('http'):
url = 'http://' + url
parts = urllib.parse.urlparse(url)
cls = http.client.HTTPConnection
if parts.scheme == 'https':
cls = http.client.HTTPSConnection
hconn = cls(parts.hostname, parts.port)
client_headers = {}
if user_agent:
client_headers['User-Agent'] = user_agent
hconn.request('GET', parts.path or '/', headers=client_headers)
return hconn.getresponse().getheaders()
def main():
url = None
agent = None
nargs = len(sys.argv) - 1
if nargs == 0:
url = url_from_clipboard()
elif nargs == 1:
url = sys.argv[1]
else:
agent = AGENTS.get(sys.argv[1])
url = sys.argv[2]
if not url:
return
try:
headers = get_headers(url, agent)
headers.sort()
headers.insert(0, ('Url', url))
for k, v in headers:
print('{0:>20}: {1}'.format(k, wrap(v, 23)))
except Exception as e:
print("** error", e)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment