#!/usr/bin/env python
"""http_headers.py
"""
import optparse, pprint, sys, urllib2

USAGE = "usage; %prog [options] URL"

class Request(urllib2.Request):
    def __init__(self, **kw):
        self.method = kw.get('method')

    def get_method(self):
        if self.method:
            return self.method
        else:
            return urllib2.Request(self)
            

parser = optparse.OptionParser(USAGE)
pao = parser.add_option
pao('-b', '--body', action="store_true", dest="body",
    help="Show the response body (normally suppressed)")
pao('-m', '--method', action="store", dest="method",
    help="HTTP method (GET, POST, etc)")

opts, args = parser.parse_args()

if len(args) != 1:
    parser.error("wrong number of command-line arguments")

url = args[0]
req = urllib2.Request(url)
if opts.method:
    req.method = opts.method.upper()
f = urllib2.urlopen(req)
for key, value in f.info().items():
    print "%s: %s" % (key.upper(), value)
if opts.body:
    print
    print f.read()
