-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-http-get-params
executable file
·39 lines (27 loc) · 1.05 KB
/
extract-http-get-params
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
import argparse
import sys
from urllib.parse import urlparse, parse_qsl
def main(args, input_file):
params, skip_empty = args.params, args.skip_empty
for line in input_file:
parsed = urlparse(line)
query = parsed.query
query_params = parse_qsl(query)
filtered = [(k, v) for k, v in query_params if k in params]
if skip_empty and len(filtered) == 0:
continue
if args.strip_values:
filtered = [(k, v.strip()) for k, v in filtered]
print('\t'.join(k + '=' + v for k, v in filtered))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('params', nargs='+')
parser.add_argument('--skip-empty', action='store_true', default=False)
parser.add_argument('--strip-values', action='store_true', default=False)
parser.add_argument('--input', nargs='?', type=argparse.FileType('r'))
args = parser.parse_args()
if not args.input:
args.input = [sys.stdin]
for i in args.input:
main(args, i)