##// END OF EJS Templates
py3: encode JSON str to bytes...
Gregory Szorc -
r40190:fe11fc7e default
parent child Browse files
Show More
@@ -1,112 +1,113 b''
1 1 #!/usr/bin/env python
2 2
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 6 from __future__ import absolute_import
7 7
8 8 import argparse
9 9 import json
10 10 import os
11 11 import sys
12 12
13 13 from mercurial import (
14 pycompat,
14 15 util,
15 16 )
16 17
17 18 httplib = util.httplib
18 19
19 20 try:
20 21 import msvcrt
21 22 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
22 23 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
23 24 except ImportError:
24 25 pass
25 26
26 27 stdout = getattr(sys.stdout, 'buffer', sys.stdout)
27 28
28 29 parser = argparse.ArgumentParser()
29 30 parser.add_argument('--twice', action='store_true')
30 31 parser.add_argument('--headeronly', action='store_true')
31 32 parser.add_argument('--json', action='store_true')
32 33 parser.add_argument('--hgproto')
33 34 parser.add_argument('--requestheader', nargs='*', default=[],
34 35 help='Send an additional HTTP request header. Argument '
35 36 'value is <header>=<value>')
36 37 parser.add_argument('--bodyfile',
37 38 help='Write HTTP response body to a file')
38 39 parser.add_argument('host')
39 40 parser.add_argument('path')
40 41 parser.add_argument('show', nargs='*')
41 42
42 43 args = parser.parse_args()
43 44
44 45 twice = args.twice
45 46 headeronly = args.headeronly
46 47 formatjson = args.json
47 48 hgproto = args.hgproto
48 49 requestheaders = args.requestheader
49 50
50 51 tag = None
51 52 def request(host, path, show):
52 53 assert not path.startswith('/'), path
53 54 global tag
54 55 headers = {}
55 56 if tag:
56 57 headers['If-None-Match'] = tag
57 58 if hgproto:
58 59 headers['X-HgProto-1'] = hgproto
59 60
60 61 for header in requestheaders:
61 62 key, value = header.split('=', 1)
62 63 headers[key] = value
63 64
64 65 conn = httplib.HTTPConnection(host)
65 66 conn.request("GET", '/' + path, None, headers)
66 67 response = conn.getresponse()
67 68 stdout.write(b'%d %s\n' % (response.status,
68 69 response.reason.encode('ascii')))
69 70 if show[:1] == ['-']:
70 71 show = sorted(h for h, v in response.getheaders()
71 72 if h.lower() not in show)
72 73 for h in [h.lower() for h in show]:
73 74 if response.getheader(h, None) is not None:
74 75 stdout.write(b"%s: %s\n" % (h.encode('ascii'),
75 76 response.getheader(h).encode('ascii')))
76 77 if not headeronly:
77 78 stdout.write(b'\n')
78 79 data = response.read()
79 80
80 81 if args.bodyfile:
81 82 bodyfh = open(args.bodyfile, 'wb')
82 83 else:
83 84 bodyfh = stdout
84 85
85 86 # Pretty print JSON. This also has the beneficial side-effect
86 87 # of verifying emitted JSON is well-formed.
87 88 if formatjson:
88 89 # json.dumps() will print trailing newlines. Eliminate them
89 90 # to make tests easier to write.
90 91 data = json.loads(data)
91 92 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
92 93 for line in lines:
93 bodyfh.write(line.rstrip())
94 bodyfh.write(pycompat.sysbytes(line.rstrip()))
94 95 bodyfh.write(b'\n')
95 96 else:
96 97 bodyfh.write(data)
97 98
98 99 if args.bodyfile:
99 100 bodyfh.close()
100 101
101 102 if twice and response.getheader('ETag', None):
102 103 tag = response.getheader('ETag')
103 104
104 105 return response.status
105 106
106 107 status = request(args.host, args.path, args.show)
107 108 if twice:
108 109 status = request(args.host, args.path, args.show)
109 110
110 111 if 200 <= status <= 305:
111 112 sys.exit(0)
112 113 sys.exit(1)
General Comments 0
You need to be logged in to leave comments. Login now