🇺🇦 Proudly Ukrainian — every project with us helps support Ukraine's defense and lasting peace in Europe.

Meet the team →
← All articles

June 6, 2024

SX Domain Validator

Discover how we built a robust domain validator utility using Python.

SX Domain Validator

Python has long been a favorite among developers for its simplicity, readability, and powerful libraries. When our team set out to develop a utility for Domain Validator, Python was the obvious choice due to its rich ecosystem and ease of use.

This script leverages the GoDaddy API to perform real-time checks and provides informative results.

Requirements:

  • Python 3
  • requests library (pip install requests)
  • A GoDaddy developer account with an API key and secret.

Prerequisites

Register here https://developer.godaddy.com/ and create the key:secret combination here: https://developer.godaddy.com/keys

Update credentials here:

headers = {
    'accept': 'application/json',
    'Authorization': 'sso-key YOUR_API_KEY:YOUR_API_SECRET'
}

Please refer to the comments in domain-validation.py.

How it works:

  1. The script reads the list of names from the names.txt file.
  2. It then creates a list of domains to check by adding the prefix (all possible letter combinations) to each name in names.txt file. The default lengh of prefix is 2 letters.
  3. The results of the domain availability check are written to the results.json

How to run

python3 domain-validation.py

For running utilitie, please save file domain-validation.py, with this code:

import requests
import string
import itertools
import json
import time

# Example of function. Must be polymorphic and has the same output format
def godaddy(domain, token):
    
    url = 'https://api.ote-godaddy.com/v1/domains/available'

    params = {
        "domain": f"{domain}",
        # Optimize for time ('FAST') or accuracy ('FULL')
        "checkType": "FAST",
        # Whether or not to include domains available for transfer. If set to True, checkType is ignored
        "forTransfer": "false"
    }
     
    headers = {
        "accept": "application/json",
        "Authorization": f"sso-key {token}"
    }
    
    # Make the GET request
    def make_request():
        response = requests.get(url, headers=headers, params=params)
        if response.status_code == 200: # OK
            return response.json()
        elif response.status_code == 429: # Too many requests
            print("Requests number exceeded. Pausing and retrying request...")
            time.sleep(1)
            return make_request()  # Retry after waiting
        else:
            return response.status_code
    try:
        response_json = make_request()
    except:
        print("Temporary failure in establishing a new connection. Pausing and retrying request...")
        time.sleep(2)
        response_json = make_request()

    if isinstance(response_json, dict):
        if "available" in response_json:
            if response_json["available"]:
                return "Available"
            else:
                return "Exists"
    else:
        return (f"Error: {response_json}")

# Generates combination of domain names
def generate_domain_names(base_domain, repeat):
    letters = string.ascii_lowercase
    combinations = itertools.product(letters, repeat=repeat)
    domain_list = [f"{''.join(combo)}{base_domain}" for combo in combinations]
    return domain_list

def main():

    results = {}
    i = 1

    key = open(".env", "r")
    token = key.readline().strip()
    if not token:
        print ("WARNING! Please add credentials to .env file!")
        exit()

    f = open("names.txt", "r")
    if not f.readline().strip():
        print ("WARNING! Add at least one name to names.txt file!")
        exit()
    
    f = open("names.txt", "r")

    for base_domain in f.readlines():
        # Here you can change the size of prefix, current value is 2
        domains_to_check = generate_domain_names(base_domain.strip(), 2)
        print (f"Number of items to check: {len(domains_to_check)}.")
        
        for domain in domains_to_check:
            #time.sleep(1)
            status = godaddy(domain, token)
            results[domain] = status
            print(f"{i}.{domain}: {status}")
            i+=1

    with open("results.json", "w") as f:
        json.dump(results, f, indent=4)

    print ("The results are saved to results.json")

if __name__ == "__main__":
    main()

Feel free to reach out with any questions or share your own experiences with Python https://skynix.co/ 📩

Related articles

Tell us what you're building

03
Oleksii, mobile app developer at Skynix

"I'm Oleksii. I work hands-on with our team here at Skynix, turning ideas into real, working products. Tell me what you're building — I read every request myself and reply within one business day."

Oleksii Prozhoha — Skynix, working hands-on with the team
What do you need?
Attach a wireframe or screenshot — PNG or JPG up to 5 MB.

Read by Oleksii · reply within one business day.

bracket icon
bracket icon
bracket icon
bracket icon
bracket icon
bracket icon
slash icon
slash icon
slash icon
slash icon
slash icon
slash icon
bracket icon
bracket icon
bracket icon
bracket icon
bracket icon
bracket icon