Pulling the host name out of a URL keeps logging, routing, allowlist checks, and endpoint comparisons focused on the network destination instead of the full string with credentials, ports, paths, queries, or fragments.
Python exposes URL parsing through urllib.parse. Current Python documentation says urlsplit() should generally be used instead of urlparse() unless path parameters must be split separately, and the returned SplitResult.hostname attribute exposes the normalized host while netloc keeps the original authority section.
A hostname is available only when the input includes a network location. Absolute URLs work directly, and scheme-less network URLs still need a leading // or Python will treat the value as a path. The masked examples below keep the shape of a credentialed API endpoint, a scheme-less service URL, and a hostless mail target, while urlsplit() still requires explicit scheme and host checks when the result affects security decisions.
Related: Get an IP address from a hostname
Related: Get a hostname from an IP address
$ python3 Python 3.14.3 >>>
>>> from urllib.parse import urlsplit
>>> parsed = urlsplit('https://svc_reader:REDACTED@API-01.Example.NET:8443/v1/health?trace=1#status')
>>> parsed
SplitResult(scheme='https', netloc='svc_reader:REDACTED@API-01.Example.NET:8443', path='/v1/health', query='trace=1', fragment='status')
Current Python documentation says urlsplit() should generally be used instead of urlparse() unless the extra path-parameter split from urlparse() is required.
>>> parsed.hostname 'api-01.example.net'
hostname strips credentials and the port number, and normalizes the host name to lowercase.
>>> parsed.netloc 'svc_reader:REDACTED@API-01.Example.NET:8443'
Use netloc for the original authority string and hostname for host-only matching, logging, or filtering.
>>> urlsplit('//edge-api.example.net/v1/health').hostname
'edge-api.example.net'
>>> urlsplit('edge-api.example.net/v1/health').hostname
None
Without the leading //, Python parses the value as a path and no hostname is returned.
#!/usr/bin/env python3 from urllib.parse import urlsplit import sys if len(sys.argv) != 2: print(f"Usage: {sys.argv[0]} <url>") raise SystemExit(1) url = sys.argv[1] parsed = urlsplit(url) if parsed.hostname is None: print(f"No host name found in URL: {url}") raise SystemExit(1) print(parsed.hostname)
The explicit None check keeps hostless URLs such as mailto:alerts@example.net from being treated as successful matches.
$ python3 get-host-name-from-url.py 'https://svc_reader:REDACTED@API-01.Example.NET:8443/v1/health?trace=1#status' api-01.example.net
$ python3 get-host-name-from-url.py 'mailto:alerts@example.net' No host name found in URL: mailto:alerts@example.net $ echo $? 1
The non-zero exit status makes the failure path easier to detect from shell scripts and automation.