make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / url-parts
blob8565fb998b070de5e9052768323c978edbdf841a
1 #!/usr/bin/env python
3 """
4 =pod
6 =head1 NAME
8 url-parts - Extract specified parts from URLs given in input stream
10 =head1 SYNOPSIS
12 echo <URL> | url-parts <PART> [<PART> [<PART> [...]]]
14 =head1 DESCRIPTION
16 Supported parts:
17 fragment, hostname, netloc, password, path, port, query, scheme, username,
18 and query.I<NAME> for the query parameter I<NAME>,
19 and query.I<NAME>.I<N> for I<N>th element of the array parameter I<NAME>.
21 Run C<url-parts --help> for the definitive list of URL part names
22 supported by the python urlparse module installed on your system.
24 =cut
26 """
28 from __future__ import print_function
29 import sys
30 import re
31 import urlparse
33 delimiter = '\t'
35 if '--help' in sys.argv:
36 sample = urlparse.urlsplit('')
37 print("Usage: echo URL | url-parts [PART-1 [PART-2 [...]]]")
38 print("PART-N is one of:", ' '.join([attr for attr in dir(sample)
39 if not attr.startswith('_') and not hasattr(getattr(sample, attr), '__call__')]))
40 print("or 'query.PARAM' to access individual query string parameters,")
41 print("or 'query.PARAM.N' where N is an integer to access array-like parameters.")
42 sys.exit(0)
44 while True:
45 line = sys.stdin.readline()
46 if line == '':
47 break
48 line = line.strip()
49 url = urlparse.urlsplit(line)
50 was_output = False
51 for param_name, param_values in urlparse.parse_qs(url.query).iteritems():
52 field_name = 'query.%s' % param_name
53 if len(param_values) == 1:
54 setattr(url, field_name, param_values[0])
55 for index, param_value in enumerate(param_values):
56 setattr(url, '%s.%d' % (field_name, index), param_value)
57 for arg in sys.argv[1:]:
58 if hasattr(url, arg):
59 value = getattr(url, arg)
60 else:
61 value = ''
62 if was_output:
63 print(delimiter, end='')
64 print(value, end='')
65 was_output = True
66 print('')