Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 4f23977

Browse files
committed
Add utility function to properly handle and split hostnames and IPs from ports that handles IPv6
1 parent a9999af commit 4f23977

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

hyper/common/util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,19 @@ def to_bytestring_tuple(*x):
2525
tuple. Uses ``to_bytestring``.
2626
"""
2727
return tuple(imap(to_bytestring, x))
28+
29+
def to_host_port_tuple(host_port_str, default_port=80)
30+
"""
31+
Converts the given string containing a host and possibly a port
32+
to a tuple.
33+
"""
34+
try:
35+
host, port = host_port_str.rsplit(':', 1)
36+
except ValueError:
37+
host, port = host_port_str, default_port
38+
else:
39+
port = int(port)
40+
41+
host = host.strip('[]')
42+
43+
return tuple(host, port)

0 commit comments

Comments
 (0)