comparison tests/get-with-headers.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents fe11fc7e541f
children 579672b347d2
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
17 17
18 httplib = util.httplib 18 httplib = util.httplib
19 19
20 try: 20 try:
21 import msvcrt 21 import msvcrt
22
22 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) 23 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
23 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) 24 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
24 except ImportError: 25 except ImportError:
25 pass 26 pass
26 27
29 parser = argparse.ArgumentParser() 30 parser = argparse.ArgumentParser()
30 parser.add_argument('--twice', action='store_true') 31 parser.add_argument('--twice', action='store_true')
31 parser.add_argument('--headeronly', action='store_true') 32 parser.add_argument('--headeronly', action='store_true')
32 parser.add_argument('--json', action='store_true') 33 parser.add_argument('--json', action='store_true')
33 parser.add_argument('--hgproto') 34 parser.add_argument('--hgproto')
34 parser.add_argument('--requestheader', nargs='*', default=[], 35 parser.add_argument(
35 help='Send an additional HTTP request header. Argument ' 36 '--requestheader',
36 'value is <header>=<value>') 37 nargs='*',
37 parser.add_argument('--bodyfile', 38 default=[],
38 help='Write HTTP response body to a file') 39 help='Send an additional HTTP request header. Argument '
40 'value is <header>=<value>',
41 )
42 parser.add_argument('--bodyfile', help='Write HTTP response body to a file')
39 parser.add_argument('host') 43 parser.add_argument('host')
40 parser.add_argument('path') 44 parser.add_argument('path')
41 parser.add_argument('show', nargs='*') 45 parser.add_argument('show', nargs='*')
42 46
43 args = parser.parse_args() 47 args = parser.parse_args()
47 formatjson = args.json 51 formatjson = args.json
48 hgproto = args.hgproto 52 hgproto = args.hgproto
49 requestheaders = args.requestheader 53 requestheaders = args.requestheader
50 54
51 tag = None 55 tag = None
56
57
52 def request(host, path, show): 58 def request(host, path, show):
53 assert not path.startswith('/'), path 59 assert not path.startswith('/'), path
54 global tag 60 global tag
55 headers = {} 61 headers = {}
56 if tag: 62 if tag:
63 headers[key] = value 69 headers[key] = value
64 70
65 conn = httplib.HTTPConnection(host) 71 conn = httplib.HTTPConnection(host)
66 conn.request("GET", '/' + path, None, headers) 72 conn.request("GET", '/' + path, None, headers)
67 response = conn.getresponse() 73 response = conn.getresponse()
68 stdout.write(b'%d %s\n' % (response.status, 74 stdout.write(
69 response.reason.encode('ascii'))) 75 b'%d %s\n' % (response.status, response.reason.encode('ascii'))
76 )
70 if show[:1] == ['-']: 77 if show[:1] == ['-']:
71 show = sorted(h for h, v in response.getheaders() 78 show = sorted(
72 if h.lower() not in show) 79 h for h, v in response.getheaders() if h.lower() not in show
80 )
73 for h in [h.lower() for h in show]: 81 for h in [h.lower() for h in show]:
74 if response.getheader(h, None) is not None: 82 if response.getheader(h, None) is not None:
75 stdout.write(b"%s: %s\n" % (h.encode('ascii'), 83 stdout.write(
76 response.getheader(h).encode('ascii'))) 84 b"%s: %s\n"
85 % (h.encode('ascii'), response.getheader(h).encode('ascii'))
86 )
77 if not headeronly: 87 if not headeronly:
78 stdout.write(b'\n') 88 stdout.write(b'\n')
79 data = response.read() 89 data = response.read()
80 90
81 if args.bodyfile: 91 if args.bodyfile:
102 if twice and response.getheader('ETag', None): 112 if twice and response.getheader('ETag', None):
103 tag = response.getheader('ETag') 113 tag = response.getheader('ETag')
104 114
105 return response.status 115 return response.status
106 116
117
107 status = request(args.host, args.path, args.show) 118 status = request(args.host, args.path, args.show)
108 if twice: 119 if twice:
109 status = request(args.host, args.path, args.show) 120 status = request(args.host, args.path, args.show)
110 121
111 if 200 <= status <= 305: 122 if 200 <= status <= 305: