Mercurial > public > mercurial-scm > hg
comparison tests/get-with-headers.py @ 36576:cfd0c1df5e33
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.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Fri, 02 Mar 2018 13:50:31 -0500 |
parents | c95c8ab2e7ec |
children | fe11fc7e541f |
comparison
equal
deleted
inserted
replaced
36575:df7b7d5033a5 | 36576:cfd0c1df5e33 |
---|---|
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 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 |
10 import os | 10 import os |
11 import sys | 11 import sys |
20 import msvcrt | 20 import msvcrt |
21 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | 21 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
22 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | 22 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
23 except ImportError: | 23 except ImportError: |
24 pass | 24 pass |
25 | |
26 stdout = getattr(sys.stdout, 'buffer', sys.stdout) | |
25 | 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') |
29 parser.add_argument('--json', action='store_true') | 31 parser.add_argument('--json', action='store_true') |
60 headers[key] = value | 62 headers[key] = value |
61 | 63 |
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. |
83 if formatjson: | 87 if formatjson: |
84 # json.dumps() will print trailing newlines. Eliminate them | 88 # json.dumps() will print trailing newlines. Eliminate them |