Show More
@@ -1,88 +1,86 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | |
|
3 | 3 | """This does HTTP GET requests given a host:port and path and returns |
|
4 | 4 | a subset of the headers plus the body of the result.""" |
|
5 | 5 | |
|
6 | 6 | import httplib, sys |
|
7 | 7 | |
|
8 | 8 | try: |
|
9 | 9 | import json |
|
10 | 10 | except ImportError: |
|
11 | 11 | try: |
|
12 | 12 | import simplejson as json |
|
13 | 13 | except ImportError: |
|
14 | 14 | json = None |
|
15 | 15 | |
|
16 | 16 | try: |
|
17 | 17 | import msvcrt, os |
|
18 | 18 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
|
19 | 19 | msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
|
20 | 20 | except ImportError: |
|
21 | 21 | pass |
|
22 | 22 | |
|
23 | 23 | twice = False |
|
24 | 24 | if '--twice' in sys.argv: |
|
25 | 25 | sys.argv.remove('--twice') |
|
26 | 26 | twice = True |
|
27 | 27 | headeronly = False |
|
28 | 28 | if '--headeronly' in sys.argv: |
|
29 | 29 | sys.argv.remove('--headeronly') |
|
30 | 30 | headeronly = True |
|
31 | 31 | formatjson = False |
|
32 | 32 | if '--json' in sys.argv: |
|
33 | 33 | sys.argv.remove('--json') |
|
34 | 34 | formatjson = True |
|
35 | 35 | |
|
36 | reasons = {'Not modified': 'Not Modified'} # python 2.4 | |
|
37 | ||
|
38 | 36 | tag = None |
|
39 | 37 | def request(host, path, show): |
|
40 | 38 | assert not path.startswith('/'), path |
|
41 | 39 | global tag |
|
42 | 40 | headers = {} |
|
43 | 41 | if tag: |
|
44 | 42 | headers['If-None-Match'] = tag |
|
45 | 43 | |
|
46 | 44 | conn = httplib.HTTPConnection(host) |
|
47 | 45 | conn.request("GET", '/' + path, None, headers) |
|
48 | 46 | response = conn.getresponse() |
|
49 |
print response.status, |
|
|
47 | print response.status, response.reason | |
|
50 | 48 | if show[:1] == ['-']: |
|
51 | 49 | show = sorted(h for h, v in response.getheaders() |
|
52 | 50 | if h.lower() not in show) |
|
53 | 51 | for h in [h.lower() for h in show]: |
|
54 | 52 | if response.getheader(h, None) is not None: |
|
55 | 53 | print "%s: %s" % (h, response.getheader(h)) |
|
56 | 54 | if not headeronly: |
|
57 | 55 | |
|
58 | 56 | data = response.read() |
|
59 | 57 | |
|
60 | 58 | # Pretty print JSON. This also has the beneficial side-effect |
|
61 | 59 | # of verifying emitted JSON is well-formed. |
|
62 | 60 | if formatjson: |
|
63 | 61 | if not json: |
|
64 | 62 | print 'no json module not available' |
|
65 | 63 | print 'did you forget a #require json?' |
|
66 | 64 | sys.exit(1) |
|
67 | 65 | |
|
68 | 66 | # json.dumps() will print trailing newlines. Eliminate them |
|
69 | 67 | # to make tests easier to write. |
|
70 | 68 | data = json.loads(data) |
|
71 | 69 | lines = json.dumps(data, sort_keys=True, indent=2).splitlines() |
|
72 | 70 | for line in lines: |
|
73 | 71 | print line.rstrip() |
|
74 | 72 | else: |
|
75 | 73 | sys.stdout.write(data) |
|
76 | 74 | |
|
77 | 75 | if twice and response.getheader('ETag', None): |
|
78 | 76 | tag = response.getheader('ETag') |
|
79 | 77 | |
|
80 | 78 | return response.status |
|
81 | 79 | |
|
82 | 80 | status = request(sys.argv[1], sys.argv[2], sys.argv[3:]) |
|
83 | 81 | if twice: |
|
84 | 82 | status = request(sys.argv[1], sys.argv[2], sys.argv[3:]) |
|
85 | 83 | |
|
86 | 84 | if 200 <= status <= 305: |
|
87 | 85 | sys.exit(0) |
|
88 | 86 | sys.exit(1) |
General Comments 0
You need to be logged in to leave comments.
Login now