vish4071
vish4071

Reputation: 5277

Custom whitelisting domain names - Python3

In my project, I want to whitelist a set of domains to handle requests. It should allow all requests from the listed domain, its sub-domains and different pages on the domain.

So, if, for example, one of the whitelisted domains is example.com, it should serve requests for www.example.com, abc.example.com, https://abc.def.example.com, example.com/pg1 etc.

Which is the best utility/ library that can be used for this purpose? Or, do I need to write my own regex?

Upvotes: 3

Views: 1590

Answers (3)

bandirom
bandirom

Reputation: 56

I suggest to use python lib tldextract.

Simple validation:

import tldextract

def validate_whitelist_url(url: str) -> bool:
    whitelist_hosts = ["domain.com", "gmail.com"]
    extracted_hosts = (tldextract.extract(host) for host in whitelist_hosts)

    _url = tldextract.extract(url)

    for host in extracted_hosts:
        if _url.subdomain == host.subdomain and _url.domain == host.domain and _url.suffix == host.suffix:
            return True
    return False

You can customize validation like you want, because you have separated data about domain, suffix, subdomain

Upvotes: 0

falloutx
falloutx

Reputation: 81

You can use the following regex to match subdomains of the domain example.com.

^([a-zA-Z0-9]+\.)*example\.com\/?.*

Upvotes: 1

Mayank Garg
Mayank Garg

Reputation: 1

You may use this python function to check if a url should be allowed based on your domain:

def isDomainAllowed(url)
  domain = 'example.com'
  match = re.search(r'example.com', url)
  if match and match.group() == domain:
    return True
  return False

Upvotes: 0

Related Questions