Checks if the host name also contains a port and separates the two. Returns either (host, None) or (host, port). Detects if host is an ipv6 address like "[::]" and removes the brackets from it.
(host_port)
| 34 | |
| 35 | |
| 36 | def split_host_port(host_port): |
| 37 | """Checks if the host name also contains a port and separates the two. |
| 38 | Returns either (host, None) or (host, port). Detects if host is an ipv6 address |
| 39 | like "[::]" and removes the brackets from it. |
| 40 | """ |
| 41 | is_ipv6_address = host_port[0] == "[" |
| 42 | if is_ipv6_address: |
| 43 | parts = host_port[1:].split("]") |
| 44 | if len(parts) == 1 or not parts[1]: |
| 45 | return (parts[0], None) |
| 46 | return (parts[0], int(parts[1][1:])) |
| 47 | else: |
| 48 | parts = host_port.split(":") |
| 49 | if len(parts) == 1: |
| 50 | return (parts[0], None) |
| 51 | return (parts[0], int(parts[1])) |
| 52 | |
| 53 | |
| 54 | def to_host_port(host, port): |