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