From 3f17106d1cbf991ded599f5773898bfc02237ce4 Mon Sep 17 00:00:00 2001 From: Yi EungJun Date: Sat, 20 Aug 2016 16:26:12 +0900 Subject: [PATCH] Make urllib.parse runnable as a script For example: $ python -m urllib.parse http://example.org:8080/foo/bar scheme=http hostname=example.org port=8080 path=/foo/bar --- Lib/urllib/parse.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 99a6977..3ca0c40 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -30,6 +30,7 @@ test_urlparse.py provides a good indicator of parsing behavior. import re import sys import collections +import argparse __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag", "urlsplit", "urlunsplit", "urlencode", "parse_qs", @@ -1006,3 +1007,16 @@ def splitvalue(attr): """splitvalue('attr=value') --> 'attr', 'value'.""" attr, delim, value = attr.partition('=') return attr, (value if delim else None) + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('url', type=str) + args = parser.parse_args() + parsed = urlparse(args.url) + if (not parsed.scheme) and (not args.url.startswith("/")): + parsed = urlparse('//' + args.url) + keys = ['scheme', 'hostname', 'port', 'path', 'params', 'query', 'fragment'] + for key in keys: + value = getattr(parsed, key) + if value: + print("%s=%s" % (key, value)) -- 2.9.3