tests/get-with-headers.py
branchstable
changeset 1052 5a3bcda2de29
parent 1051 5b75af0ca224
child 1053 ede95d3a1f27
equal deleted inserted replaced
1051:5b75af0ca224 1052:5a3bcda2de29
    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 tag = None
       
    21 def request(host, path, show):
       
    22 
       
    23     global tag
       
    24     headers = {}
       
    25     if tag:
       
    26         headers['If-None-Match'] = tag
       
    27 
       
    28     conn = httplib.HTTPConnection(host)
       
    29     conn.request("GET", path, None, headers)
       
    30     response = conn.getresponse()
       
    31     print response.status, response.reason
       
    32     for h in [h.lower() for h in show]:
       
    33         if response.getheader(h, None) is not None:
       
    34             print "%s: %s" % (h, response.getheader(h))
       
    35 
       
    36     print
       
    37     data = response.read()
       
    38     sys.stdout.write(data)
       
    39 
       
    40     if twice and response.getheader('ETag', None):
       
    41         tag = response.getheader('ETag')
       
    42 
       
    43     return response.status
       
    44 
       
    45 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
       
    46 if twice:
       
    47     status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
       
    48 
       
    49 if 200 <= status <= 305:
    28     sys.exit(0)
    50     sys.exit(0)
    29 sys.exit(1)
    51 sys.exit(1)