##// END OF EJS Templates
get-with-headers: don't block indefinitely if the server had an internal error...
Javi Merino -
r19865:ba6577a1 default
parent child Browse files
Show More
@@ -1,60 +1,61 b''
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 headeronly = False
19 headeronly = False
20 if '--headeronly' in sys.argv:
20 if '--headeronly' in sys.argv:
21 sys.argv.remove('--headeronly')
21 sys.argv.remove('--headeronly')
22 headeronly = True
22 headeronly = True
23
23
24 reasons = {'Not modified': 'Not Modified'} # python 2.4
24 reasons = {'Not modified': 'Not Modified'} # python 2.4
25
25
26 tag = None
26 tag = None
27 def request(host, path, show):
27 def request(host, path, show):
28 assert not path.startswith('/'), path
28 assert not path.startswith('/'), path
29 global tag
29 global tag
30 headers = {}
30 headers = {}
31 if tag:
31 if tag:
32 headers['If-None-Match'] = tag
32 headers['If-None-Match'] = tag
33
33
34 conn = httplib.HTTPConnection(host)
34 conn = httplib.HTTPConnection(host)
35 conn.request("GET", '/' + path, None, headers)
35 conn.request("GET", '/' + path, None, headers)
36 response = conn.getresponse()
36 response = conn.getresponse()
37 print response.status, reasons.get(response.reason, response.reason)
37 print response.status, reasons.get(response.reason, response.reason)
38 if show[:1] == ['-']:
38 if show[:1] == ['-']:
39 show = sorted(h for h, v in response.getheaders()
39 show = sorted(h for h, v in response.getheaders()
40 if h.lower() not in show)
40 if h.lower() not in show)
41 for h in [h.lower() for h in show]:
41 for h in [h.lower() for h in show]:
42 if response.getheader(h, None) is not None:
42 if response.getheader(h, None) is not None:
43 print "%s: %s" % (h, response.getheader(h))
43 print "%s: %s" % (h, response.getheader(h))
44 if not headeronly:
44 if not headeronly:
45 print
45 print
46 data = response.read()
46 if response.status != 500:
47 sys.stdout.write(data)
47 data = response.read()
48 sys.stdout.write(data)
48
49
49 if twice and response.getheader('ETag', None):
50 if twice and response.getheader('ETag', None):
50 tag = response.getheader('ETag')
51 tag = response.getheader('ETag')
51
52
52 return response.status
53 return response.status
53
54
54 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
55 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
55 if twice:
56 if twice:
56 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
57 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
57
58
58 if 200 <= status <= 305:
59 if 200 <= status <= 305:
59 sys.exit(0)
60 sys.exit(0)
60 sys.exit(1)
61 sys.exit(1)
General Comments 0
You need to be logged in to leave comments. Login now