Custom whitelisting domain names - Python3

Viewed 1011

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?

2 Answers

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

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

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
Related