tests/get-with-headers.py
changeset 24543 747401086a38
parent 23409 dc4d2cd3aa3e
child 25208 5a6820f8da4d
equal deleted inserted replaced
24542:9fbda55c68d7 24543:747401086a38
     2 
     2 
     3 """This does HTTP GET requests given a host:port and path and returns
     3 """This does HTTP GET requests given a host:port and path and returns
     4 a subset of the headers plus the body of the result."""
     4 a subset of the headers plus the body of the result."""
     5 
     5 
     6 import httplib, sys
     6 import httplib, sys
       
     7 
       
     8 try:
       
     9     import json
       
    10 except ImportError:
       
    11     try:
       
    12         import simplejson as json
       
    13     except ImportError:
       
    14         json = None
     7 
    15 
     8 try:
    16 try:
     9     import msvcrt, os
    17     import msvcrt, os
    10     msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    18     msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    11     msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
    19     msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
    18     twice = True
    26     twice = True
    19 headeronly = False
    27 headeronly = False
    20 if '--headeronly' in sys.argv:
    28 if '--headeronly' in sys.argv:
    21     sys.argv.remove('--headeronly')
    29     sys.argv.remove('--headeronly')
    22     headeronly = True
    30     headeronly = True
       
    31 formatjson = False
       
    32 if '--json' in sys.argv:
       
    33     sys.argv.remove('--json')
       
    34     formatjson = True
    23 
    35 
    24 reasons = {'Not modified': 'Not Modified'} # python 2.4
    36 reasons = {'Not modified': 'Not Modified'} # python 2.4
    25 
    37 
    26 tag = None
    38 tag = None
    27 def request(host, path, show):
    39 def request(host, path, show):
    42         if response.getheader(h, None) is not None:
    54         if response.getheader(h, None) is not None:
    43             print "%s: %s" % (h, response.getheader(h))
    55             print "%s: %s" % (h, response.getheader(h))
    44     if not headeronly:
    56     if not headeronly:
    45         print
    57         print
    46         data = response.read()
    58         data = response.read()
    47         sys.stdout.write(data)
    59 
       
    60         # Pretty print JSON. This also has the beneficial side-effect
       
    61         # of verifying emitted JSON is well-formed.
       
    62         if formatjson:
       
    63             if not json:
       
    64                 print 'no json module not available'
       
    65                 print 'did you forget a #require json?'
       
    66                 sys.exit(1)
       
    67 
       
    68             # json.dumps() will print trailing newlines. Eliminate them
       
    69             # to make tests easier to write.
       
    70             data = json.loads(data)
       
    71             lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
       
    72             for line in lines:
       
    73                 print line.rstrip()
       
    74         else:
       
    75             sys.stdout.write(data)
    48 
    76 
    49         if twice and response.getheader('ETag', None):
    77         if twice and response.getheader('ETag', None):
    50             tag = response.getheader('ETag')
    78             tag = response.getheader('ETag')
    51 
    79 
    52     return response.status
    80     return response.status