get-with-headers: use bytes stdout thoroughly
On Python 3, sys.stdout.buffer is backed by a separate buffer from sys.stdout.
We should choose one.
--- a/tests/get-with-headers.py Thu Mar 01 17:16:36 2018 -0800
+++ b/tests/get-with-headers.py Fri Mar 02 13:50:31 2018 -0500
@@ -3,7 +3,7 @@
"""This does HTTP GET requests given a host:port and path and returns
a subset of the headers plus the body of the result."""
-from __future__ import absolute_import, print_function
+from __future__ import absolute_import
import argparse
import json
@@ -23,6 +23,8 @@
except ImportError:
pass
+stdout = getattr(sys.stdout, 'buffer', sys.stdout)
+
parser = argparse.ArgumentParser()
parser.add_argument('--twice', action='store_true')
parser.add_argument('--headeronly', action='store_true')
@@ -62,21 +64,23 @@
conn = httplib.HTTPConnection(host)
conn.request("GET", '/' + path, None, headers)
response = conn.getresponse()
- print(response.status, response.reason)
+ stdout.write(b'%d %s\n' % (response.status,
+ response.reason.encode('ascii')))
if show[:1] == ['-']:
show = sorted(h for h, v in response.getheaders()
if h.lower() not in show)
for h in [h.lower() for h in show]:
if response.getheader(h, None) is not None:
- print("%s: %s" % (h, response.getheader(h)))
+ stdout.write(b"%s: %s\n" % (h.encode('ascii'),
+ response.getheader(h).encode('ascii')))
if not headeronly:
- print()
+ stdout.write(b'\n')
data = response.read()
if args.bodyfile:
bodyfh = open(args.bodyfile, 'wb')
else:
- bodyfh = getattr(sys.stdout, 'buffer', sys.stdout)
+ bodyfh = stdout
# Pretty print JSON. This also has the beneficial side-effect
# of verifying emitted JSON is well-formed.