Show More
@@ -1,55 +1,56 | |||||
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 |
|
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 | twice = False |
|
15 | twice = False | |
16 | if '--twice' in sys.argv: |
|
16 | if '--twice' in sys.argv: | |
17 | sys.argv.remove('--twice') |
|
17 | sys.argv.remove('--twice') | |
18 | twice = True |
|
18 | twice = True | |
19 |
|
19 | |||
20 | reasons = {'Not modified': 'Not Modified'} # python 2.4 |
|
20 | reasons = {'Not modified': 'Not Modified'} # python 2.4 | |
21 |
|
21 | |||
22 | tag = None |
|
22 | tag = None | |
23 | def request(host, path, show): |
|
23 | def request(host, path, show): | |
24 | assert not path.startswith('/'), path |
|
24 | assert not path.startswith('/'), path | |
25 | global tag |
|
25 | global tag | |
26 | headers = {} |
|
26 | headers = {} | |
27 | if tag: |
|
27 | if tag: | |
28 | headers['If-None-Match'] = tag |
|
28 | headers['If-None-Match'] = tag | |
29 |
|
29 | |||
30 | conn = httplib.HTTPConnection(host) |
|
30 | conn = httplib.HTTPConnection(host) | |
31 | conn.request("GET", '/' + path, None, headers) |
|
31 | conn.request("GET", '/' + path, None, headers) | |
32 | response = conn.getresponse() |
|
32 | response = conn.getresponse() | |
33 | print response.status, reasons.get(response.reason, response.reason) |
|
33 | print response.status, reasons.get(response.reason, response.reason) | |
34 | if show[:1] == ['-']: |
|
34 | if show[:1] == ['-']: | |
35 |
show = |
|
35 | show = sorted(h for h, v in response.getheaders() | |
|
36 | if h.lower() not in show) | |||
36 | for h in [h.lower() for h in show]: |
|
37 | for h in [h.lower() for h in show]: | |
37 | if response.getheader(h, None) is not None: |
|
38 | if response.getheader(h, None) is not None: | |
38 | print "%s: %s" % (h, response.getheader(h)) |
|
39 | print "%s: %s" % (h, response.getheader(h)) | |
39 |
|
40 | |||
40 |
|
41 | |||
41 | data = response.read() |
|
42 | data = response.read() | |
42 | sys.stdout.write(data) |
|
43 | sys.stdout.write(data) | |
43 |
|
44 | |||
44 | if twice and response.getheader('ETag', None): |
|
45 | if twice and response.getheader('ETag', None): | |
45 | tag = response.getheader('ETag') |
|
46 | tag = response.getheader('ETag') | |
46 |
|
47 | |||
47 | return response.status |
|
48 | return response.status | |
48 |
|
49 | |||
49 | status = request(sys.argv[1], sys.argv[2], sys.argv[3:]) |
|
50 | status = request(sys.argv[1], sys.argv[2], sys.argv[3:]) | |
50 | if twice: |
|
51 | if twice: | |
51 | status = request(sys.argv[1], sys.argv[2], sys.argv[3:]) |
|
52 | status = request(sys.argv[1], sys.argv[2], sys.argv[3:]) | |
52 |
|
53 | |||
53 | if 200 <= status <= 305: |
|
54 | if 200 <= status <= 305: | |
54 | sys.exit(0) |
|
55 | sys.exit(0) | |
55 | sys.exit(1) |
|
56 | sys.exit(1) |
General Comments 0
You need to be logged in to leave comments.
Login now