Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:50:29+00:00 2026-05-26T03:50:29+00:00

I have to solve this little problem in Python/Django and I’m wondering which is

  • 0

I have to solve this little problem in Python/Django and I’m wondering which is the best way to go about it.

I have a list of allowed domains e.g. www.google.com, mail.google.com. I would like to check if a domain is allowed or not. Now, a request from either www.google.com is valid, and a request from mail.google.com is valid too.

However, I would like to count www as a wildcard which means that if I had the same list of allowed domains as above, A request from docs.google.com would be considered valid and so a request from google.com even though they don’t exist in the list but since www.google.com exists and www. is a leading wildcard, both domains match.

What would be the best way to implement this? Here’s a snippet of mine where I’m trying to implement this:

def dispatch(self, request, *args, **kwargs):
    url = request.REQUEST['url']
    host = urlparse.urlparse(url).netloc.lower().strip()
    domains = [domain.host.lstrip('www.') for domain in Domain.objects.all()]

    for domain in domains:
        if host.endswith(domain):
            return super(ProcessRequestView, self).dispatch(request, *args, **kwargs)
    return HttpResponseForbidden()

My Domain model has just one field, called host. This solution of mine make just one DB hit but I’m sure if it’s the best or the most efficient.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:50:30+00:00Added an answer on May 26, 2026 at 3:50 am

    First of all if i would like to use wildcard it would not be “www” since www is nothing but a subdomain.

    For example if i would like to let all google domains my record would be “.google.com“.
    This would show for example http://www.google.com.au as not allowed. It’s possible for you to put wildcard to the end but then google.com.example.com would be allowed which is not good idea. Or maybe you would like to allow all UK sites with domain name ending with co.uk with “.co.uk” record.

    Thus you should be looking for subdomains specific to general:

    Lest assume the domain name is docs.google.co.uk and you have only the “.co.uk” record.

    1. Query for docs.google.co.uk – check whether full host name allowed or not.

    2. Query for .docs.google.co.uk in case of any wildcard definition

    3. Query for .google.co.uk

    4. Query for .co.uk – Bingo you find a wildcard, its allowed!

    Anyway, in your code you are selecting every domain objects from db and then looking for appropriate domain name with a loop. This operation would unnecessarily slow down your process. Instead of selecting them all you should let database to handle the elimination of not related domains.

    def dispatch(self, request, *args, **kwargs):
      url = request.REQUEST['url']
      host = urlparse.urlparse(url).netloc.lower().strip()
    
      domains = Domain.objects.filter(domain=host)
      if len(domains):
        return super(ProcessRequestView, self).dispatch(request, *args, **kwargs)
      else:
        newHost = '.%s' %host 
        dotPosition = -1
        for i in range(newHost.count('.')):
          dotPosition = newHost.find('.', dotPosition + 1)
          domains = Domain.objects.filter(domain=newHost[dotPosition:])
          if len(domains):
            return super(ProcessRequestView, self).dispatch(request, *args, **kwargs)
    
        return HttpResponseForbidden()
    

    I hope it will help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some ideas on how I can best solve this problem. I have
I have a little problem: I want to solve this problem with OCaml, so
I have a little problem which I have no clue how to solve. I
I have been trying to solve this problem for a while, but couldn't with
I have an idea for how to solve this problem, but I wanted to
I am trying to solve this problem. I have a series of SELECT statements
I'm hoping people have some ideas to help solve this problem. I am developing
What's a good algorithm to solve this problem? I have three groups of people
I have a long string, ex: Please help me to solve this problem. This
This is a simplified task which I have to solve in real project. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.