##// END OF EJS Templates
get-with-headers: use bytes stdout thoroughly...
Yuya Nishihara -
r36594:cfd0c1df default
parent child Browse files
Show More
@@ -3,7 +3,7 b''
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 from __future__ import absolute_import, print_function
6 from __future__ import absolute_import
7
7
8 import argparse
8 import argparse
9 import json
9 import json
@@ -23,6 +23,8 b' try:'
23 except ImportError:
23 except ImportError:
24 pass
24 pass
25
25
26 stdout = getattr(sys.stdout, 'buffer', sys.stdout)
27
26 parser = argparse.ArgumentParser()
28 parser = argparse.ArgumentParser()
27 parser.add_argument('--twice', action='store_true')
29 parser.add_argument('--twice', action='store_true')
28 parser.add_argument('--headeronly', action='store_true')
30 parser.add_argument('--headeronly', action='store_true')
@@ -62,21 +64,23 b' def request(host, path, show):'
62 conn = httplib.HTTPConnection(host)
64 conn = httplib.HTTPConnection(host)
63 conn.request("GET", '/' + path, None, headers)
65 conn.request("GET", '/' + path, None, headers)
64 response = conn.getresponse()
66 response = conn.getresponse()
65 print(response.status, response.reason)
67 stdout.write(b'%d %s\n' % (response.status,
68 response.reason.encode('ascii')))
66 if show[:1] == ['-']:
69 if show[:1] == ['-']:
67 show = sorted(h for h, v in response.getheaders()
70 show = sorted(h for h, v in response.getheaders()
68 if h.lower() not in show)
71 if h.lower() not in show)
69 for h in [h.lower() for h in show]:
72 for h in [h.lower() for h in show]:
70 if response.getheader(h, None) is not None:
73 if response.getheader(h, None) is not None:
71 print("%s: %s" % (h, response.getheader(h)))
74 stdout.write(b"%s: %s\n" % (h.encode('ascii'),
75 response.getheader(h).encode('ascii')))
72 if not headeronly:
76 if not headeronly:
73 print()
77 stdout.write(b'\n')
74 data = response.read()
78 data = response.read()
75
79
76 if args.bodyfile:
80 if args.bodyfile:
77 bodyfh = open(args.bodyfile, 'wb')
81 bodyfh = open(args.bodyfile, 'wb')
78 else:
82 else:
79 bodyfh = getattr(sys.stdout, 'buffer', sys.stdout)
83 bodyfh = stdout
80
84
81 # Pretty print JSON. This also has the beneficial side-effect
85 # Pretty print JSON. This also has the beneficial side-effect
82 # of verifying emitted JSON is well-formed.
86 # of verifying emitted JSON is well-formed.
General Comments 0
You need to be logged in to leave comments. Login now