How to easily replace host:port of a url in Python

for python3

1
2
3
4
5
6
7
from urllib.parse import urlparse

url = "http://example.com/about"
parsed = urlparse(a)
parsed = parsed._replace(netloc="localhost:8000").geturl()
print(parsed)
# 'http://localhost:8000/about'