summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1996-06-13 19:12:35 (GMT)
committerGuido van Rossum <guido@python.org>1996-06-13 19:12:35 (GMT)
commit53725a2858fe992cac4b343615b1594f201cced4 (patch)
tree2513b0164fdfdd88b8471f5489c57579d9528997 /Lib
parent2745753b91b1f83f8c24a2e3ed39f103ba1a1043 (diff)
downloadcpython-53725a2858fe992cac4b343615b1594f201cced4.zip
cpython-53725a2858fe992cac4b343615b1594f201cced4.tar.gz
cpython-53725a2858fe992cac4b343615b1594f201cced4.tar.bz2
Added splitnport(), which is like splitport() but returns a numeric port,
is forgiving about semi-numeric port numbers, and allows you to specify a default port (default is -1, None returned for nonnumeric port).
Diffstat (limited to 'Lib')
-rw-r--r--Lib/urllib.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py
index 6caf2b2..45f3e99 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -596,6 +596,23 @@ def splitport(host):
if _portprog.match(host) >= 0: return _portprog.group(1, 2)
return host, None
+# Split host and port, returning numeric port.
+# Return given default port if no ':' found; defaults to -1.
+# Return numerical port if digits are found after ':'.
+# Return None if ':' but no digits.
+_nportprog = regex.compile('^\(.*\):\([^0-9]*\([0-9]*\).*\)$')
+def splitnport(host, defport=-1):
+ if _nportprog.match(host) >= 0:
+ host, port = _nportprog.group(1, 3)
+ nport = None
+ if port:
+ try:
+ nport = string.atoi(port)
+ except string.atoi_error:
+ pass
+ return host, nport
+ return host, defport
+
_queryprog = regex.compile('^\(.*\)\?\([^?]*\)$')
def splitquery(url):
if _queryprog.match(url) >= 0: return _queryprog.group(1, 2)