Changing hostname in a url

You can use urllib.parse.urlparse function and ParseResult._replace method (Python 3):

>>> import urllib.parse
>>> parsed = urllib.parse.urlparse("https://www.google.dk:80/barbaz")
>>> replaced = parsed._replace(netloc="www.foo.dk:80")
>>> print(replaced)
ParseResult(scheme="https", netloc="www.foo.dk:80", path="/barbaz", params="", query='', fragment="")

If you’re using Python 2, then replace urllib.parse with urlparse.

ParseResult is a subclass of namedtuple and _replace is a namedtuple method that:

returns a new instance of the named tuple replacing specified fields
with new values

UPDATE:

As @2rs2ts said in the comment netloc attribute includes a port number.

Good news: ParseResult has hostname and port attributes.
Bad news: hostname and port are not the members of namedtuple, they’re dynamic properties and you can’t do parsed._replace(hostname="www.foo.dk"). It’ll throw an exception.

If you don’t want to split on : and your url always has a port number and doesn’t have username and password (that’s urls like “https://username:[email protected]:80/barbaz“) you can do:

parsed._replace(netloc="{}:{}".format(parsed.hostname, parsed.port))

Leave a Comment