Shubhangi Upadhyay
Shubhangi Upadhyay

Reputation: 27

Domain name detection

I want to get the filter of the legit domain names using this Python code. I can't see any problem with the code and have consulted a lot of websites. It is not able to separate the right and wrong domain names.

def getDomains():
    with open('domains.txt', 'r+') as f:
        for domainName in f.read().splitlines():
            domains.append(domainName)

def run():
    for dom in domains:
       if dom is not None and dom != '':
           details = pythonwhois.get_whois(dom)
           if str(details) is None:
               unavailable.append(dom)
           else:
               available.append(dom)

the result should be like:

available domains

google.com youtube.com


unavailable domains

xcdv345.hgt.com letstrythis12398.net

Upvotes: 1

Views: 660

Answers (3)

Khalid Ali
Khalid Ali

Reputation: 1224

Check if the id key exists in the returned dictionary from pythonwhois.get_whois()

Example:

import pythonwhois

domains = ['google.com', 'i-n-v-a-l-i-d.com']

for dom in domains:
  if dom is not None and dom != '':
    details = pythonwhois.get_whois(dom)

    # Check if 'id' key exists in the 'details' dictionary
    # if the 'id' key exists the domain is already registerd
    if 'id' in details:
      print('{} available'.format(dom))
    else:
      print('{} unavailable'.format(dom))

Output:

google.com available
i-n-v-a-l-i-d.com unavailable

Upvotes: 0

grahamlyons
grahamlyons

Reputation: 717

The line if str(details) is None: will always be False, even if details is None.

Running str(None) gives you the string 'None', which is not the same as the value None:

str(None) is None  # False
None is None       # True

Upvotes: 1

machine424
machine424

Reputation: 155

Even if the domain isn't registered, pythonwhois.get_whois isn't None.

Try printing the result of pythonwhois.get_whois('jjj876686.njerfjr'), for example the field contacts is always present (the result, and str(result), are different from None) (see http://cryto.net/pythonwhois/usage.html#pythonwhois_get_whoisdomainnormalized)

Upvotes: 1

Related Questions