Extract the registrable domain from a URL by reading the host right to left, and check five lookalike links against costco.com.
Extract the registrable domain from a URL by reading the host right to left, and check five lookalike links against costco.com.
Answer
def host_of(url): rest = url.split("://", 1)[-1] return rest.split("/", 1)[0].split("?", 1)[0].lower() def registrable_domain(url): return ".".join(host_of(url).split(".")[-2:]) links = [ "https://www.costco.com/warehouse-savings.html", "https://costco.wholesaledividend.com/claim", "http://costco.com.rewards-centre.net/verify", "https://shop.costco.com/cart", "https://costco-com.secure-pay.io/login", ] for link in links: reg = registrable_domain(link) ok = reg == "costco.com" print(f"{reg:24s} really Costco? {ok}")
Stallings & Brown, Computer Security 5e, ch6 §6.4–§6.5