tests/get-with-headers.py
changeset 1054 671c1a0be907
parent 1053 ede95d3a1f27
child 1106 a02b1025536c
equal deleted inserted replaced
1050:a0451f80f665 1054:671c1a0be907
     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 import httplib, sys, re
     6 import httplib, sys
     7 
     7 
     8 try:
     8 try:
     9     import msvcrt, os
     9     import msvcrt, os
    10     msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    10     msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    11     msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
    11     msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
    12 except ImportError:
    12 except ImportError:
    13     pass
    13     pass
    14 
    14 
    15 headers = [h.lower() for h in sys.argv[3:]]
    15 twice = False
    16 conn = httplib.HTTPConnection(sys.argv[1])
    16 if '--twice' in sys.argv:
    17 conn.request("GET", sys.argv[2])
    17     sys.argv.remove('--twice')
    18 response = conn.getresponse()
    18     twice = True
    19 print response.status, response.reason
       
    20 for h in headers:
       
    21     if response.getheader(h, None) is not None:
       
    22         print "%s: %s" % (h, response.getheader(h))
       
    23 print
       
    24 data = response.read()
       
    25 sys.stdout.write(data)
       
    26 
    19 
    27 if 200 <= response.status <= 299:
    20 reasons = {'Not modified': 'Not Modified'} # python 2.4
       
    21 
       
    22 tag = None
       
    23 def request(host, path, show):
       
    24 
       
    25     global tag
       
    26     headers = {}
       
    27     if tag:
       
    28         headers['If-None-Match'] = tag
       
    29 
       
    30     conn = httplib.HTTPConnection(host)
       
    31     conn.request("GET", path, None, headers)
       
    32     response = conn.getresponse()
       
    33     print response.status, reasons.get(response.reason, response.reason)
       
    34     for h in [h.lower() for h in show]:
       
    35         if response.getheader(h, None) is not None:
       
    36             print "%s: %s" % (h, response.getheader(h))
       
    37 
       
    38     print
       
    39     data = response.read()
       
    40     sys.stdout.write(data)
       
    41 
       
    42     if twice and response.getheader('ETag', None):
       
    43         tag = response.getheader('ETag')
       
    44 
       
    45     return response.status
       
    46 
       
    47 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
       
    48 if twice:
       
    49     status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
       
    50 
       
    51 if 200 <= status <= 305:
    28     sys.exit(0)
    52     sys.exit(0)
    29 sys.exit(1)
    53 sys.exit(1)