Question: ***IN PYTHON PLEASE*** Partial Hostname Extraction A Uniform Resource Locator (URL) is a unique address for a document on the World-Wide Web. A URL has
***IN PYTHON PLEASE*** Partial Hostname Extraction
A Uniform Resource Locator (URL) is a unique address for a document on the World-Wide Web. A URL has the following form:
scheme://hostname/path/filename
-scheme is a protocol such as http, ftp, irc or file -hostname consists of 1 or more alphanumeric strings (with length 1 or more) followed by single periods, followed by a top-level domain (TLD) like com, edu, or net -path is optional, and consists of 1 or more alphanumeric strings (with length 1 or more), each followed by a forward slash - filename is also optional, and is an alphanumeric string (of length 1 or more)
URL Examples
irc://foo.b25.com/readme.txt file://bar.edu/path/to/file.html ftp://12q.net/
Complete the getHost() function, which takes a single string argument representing a URL and returns a string that corresponds to the next-to-last section of the hostname. For example, given the URL "http://www.example.com/", the function would return the string "example". Given the URL "ftp://this.is.a.long.name.net/path/to/some/file.php", the function would return the string "name". While the path and filename sections of the URL are optional, you may assume that the full hostname is always followed by a single forward slash ("/").
The easiest way to solve this problem is by using find() and rfind() to strategically and systematically remove pieces of the original string: 1. Start by locating and removing the scheme section, along with its trailing "://" substring. 2. Next, locate the first forward slash and remove it and everything after it, to reduce the source string to just the (full) hostname. 3. Third, locate the last period in the hostname, and remove it and everything that follows (to eliminate the TLD). 4. If the remaining string still contains at least one period, find the last period and remove everything before it (and the last period itself).
Examples: Function Call Resulting String getHost("irc://foo.com/") foo getHost("http://i.am.a.hostname.edu/blah/blah/") hostname getHost("ftp://some-like.it-hot.net/something/filename.pdf") it-hot
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
