Get protocol + host name from URL

You should be able to do it with urlparse (docs: python2, python3):

from urllib.parse import urlparse
# from urlparse import urlparse  # Python 2
parsed_uri = urlparse('http://stackoverflow.com/questions/1234567/blah-blah-blah-blah' )
result="{uri.scheme}://{uri.netloc}/".format(uri=parsed_uri)
print(result)

# gives
'http://stackoverflow.com/'

Leave a Comment