tests/get-with-headers.py
changeset 28726 f4b31fcd5e72
parent 27296 8e86679d8acd
child 29455 0c741fd6158a
equal deleted inserted replaced
28725:3cf1995dbdd5 28726:f4b31fcd5e72
     1 #!/usr/bin/env python
     1 #!/usr/bin/env python
     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 from __future__ import absolute_import
     6 from __future__ import absolute_import, print_function
     7 
     7 
     8 import httplib
     8 import httplib
     9 import json
     9 import json
    10 import os
    10 import os
    11 import sys
    11 import sys
    39         headers['If-None-Match'] = tag
    39         headers['If-None-Match'] = tag
    40 
    40 
    41     conn = httplib.HTTPConnection(host)
    41     conn = httplib.HTTPConnection(host)
    42     conn.request("GET", '/' + path, None, headers)
    42     conn.request("GET", '/' + path, None, headers)
    43     response = conn.getresponse()
    43     response = conn.getresponse()
    44     print response.status, response.reason
    44     print(response.status, response.reason)
    45     if show[:1] == ['-']:
    45     if show[:1] == ['-']:
    46         show = sorted(h for h, v in response.getheaders()
    46         show = sorted(h for h, v in response.getheaders()
    47                       if h.lower() not in show)
    47                       if h.lower() not in show)
    48     for h in [h.lower() for h in show]:
    48     for h in [h.lower() for h in show]:
    49         if response.getheader(h, None) is not None:
    49         if response.getheader(h, None) is not None:
    50             print "%s: %s" % (h, response.getheader(h))
    50             print("%s: %s" % (h, response.getheader(h)))
    51     if not headeronly:
    51     if not headeronly:
    52         print
    52         print()
    53         data = response.read()
    53         data = response.read()
    54 
    54 
    55         # Pretty print JSON. This also has the beneficial side-effect
    55         # Pretty print JSON. This also has the beneficial side-effect
    56         # of verifying emitted JSON is well-formed.
    56         # of verifying emitted JSON is well-formed.
    57         if formatjson:
    57         if formatjson:
    58             # json.dumps() will print trailing newlines. Eliminate them
    58             # json.dumps() will print trailing newlines. Eliminate them
    59             # to make tests easier to write.
    59             # to make tests easier to write.
    60             data = json.loads(data)
    60             data = json.loads(data)
    61             lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
    61             lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
    62             for line in lines:
    62             for line in lines:
    63                 print line.rstrip()
    63                 print(line.rstrip())
    64         else:
    64         else:
    65             sys.stdout.write(data)
    65             sys.stdout.write(data)
    66 
    66 
    67         if twice and response.getheader('ETag', None):
    67         if twice and response.getheader('ETag', None):
    68             tag = response.getheader('ETag')
    68             tag = response.getheader('ETag')