How to Make a Subdomain Scanner in Python? [Complete Guide] (2024)

Posted in PROGRAMMING LANGUAGE / PYTHON

How to Make a Subdomain Scanner in Python? [Complete Guide] (1)

How to Make a Subdomain Scanner in Python? [Complete Guide] (2)

Vinay Khatri
Last updated on July 12, 2024

This tutorial details how to make a subdomain scanner in Python. Popular tech giants such as Google, Mozilla, Apple, and Meta not only have their official websites with various domain names but also support many subdomains for their various products and services.

For example, facebook.com has a subdomain https://developers.facebook.com/ that provides a platform for developers across the globe to communicate and contribute to Meta.

Similarly, the social media giant has many subdomains for all the services and products it offers.

In this Python tutorial, we will learn how to list out all the subdomains offered by a domain in Python . However, before we discuss the Python program, let's discuss the libraries and other dependencies we will be using for this tutorial.

Required Libraries and Files

1) Python requests Library

In this tutorial, we will be using the de-facto Python library for HTTP requests, i.e., the requests library to handle HTTP requests. Using requests, we will send the get request to the prospect subdomain URLs and check if the subdomain for the domain exists or not.

To install the requests library for your Python environment, run the following pip install command on your terminal or command prompt:

pip install requests

2) Python colorama Library (Optional)

colorama is an open-source Python library that is used to produce colorful terminal text. In this tutorial, we will be using this library to print the output text in a colored format. You can install the colorama library for your Python environment using the following pip install command:

pip install colorama

3) subdomains.txt File

To find out all the subdomains of a domain, we will use brute force techniques, in which we will send the GET request to all the combinations of subdomain URLs, and based on the success response, we will print the available subdomains. You can copy and paste the suffix for all possible subdomain from our GitHub repository and save it locally as subdomains.txt .

We would recommend you save the subdomains.txt file in the same directory where your Python script is located.

Now, it's time to open your best Python IDE or text editor and start coding.

How to Make a Subdomain Scanner in Python?

We will start with importing the required modules.

import requestsfrom colorama import Fore#for windowsfrom colorama import initinit()

If you are on a Windows system, you need to initialize colorama by calling the init() method. It will not have any effect on macOS and Linux. Now, let's define an identifier url that represents the domain name for which we want to find all the subdomains.

# the domain to scan for subdomainsdomain = "facebook.com"

In this tutorial, we are finding all the subdomains offered by facebook.com. Next, we will open the subdomains.txt file in the read "r" mode, read the subdomains line by line, create the subdomain URL with the help of subdomain and domain, and send GET request to the subdomain URL.

with open(filename, "r") as file: for subdomain in file.readlines(): # define subdomain url subdomain_url = f"https://{subdomain.strip()}.{domain}" try: response = requests.get(subdomain_url) #200 success code if response.status_code==200: print(Fore.GREEN +f"Subdomain Found [+]: {subdomain_url}") except: pass
  • The readlines() function will read the file line by line.
  • strip() will remove the unnecessary space and new line from the subdomain string.
  • The get() function will send the GET request to the specified URL.
  • status_code returns an integer value for the response status.

Finally, put all the code together and execute.

Python Program to Find Subdomains

import requestsfrom colorama import Fore#initialize colorama for windowsfrom colorama import initinit()# the domain to scan for subdomainsdomain = "facebook.com"#https://github.com/KHATRIVINAY1/data/blob/main/subdomains.txtfilename="subdomains.txt"with open(filename, "r") as file: for subdomain in file.readlines(): # define subdomain url subdomain_url = f"https://{subdomain.strip()}.{domain}" try: response = requests.get(subdomain_url) #200 success code if response.status_code==200: print(Fore.GREEN +f"Subdomain Found [+]: {subdomain_url}") except: pass 

Output

How to Make a Subdomain Scanner in Python? [Complete Guide] (3)

Conclusion

In this Python tutorial, we learned how to make a subdomain scanner in Python. When you execute the above program, it might take a few minutes to print out all the subdomains offered by Facebook.com.

If you want your program to run faster, you can use multithreading in Python .

People are also reading:

  • Best Python GUI Frameworks
  • Parse Data From JSON Into Python
  • Delete a File in Python
  • Update All Python Packages
  • How to loop with indexes in Python?
  • Python list vs. Tuple
  • Best Python Data Visualization Libraries
  • Python Modulo in Practice
  • Python Data Visualization
How to Make a Subdomain Scanner in Python? [Complete Guide] (2024)

FAQs

How to create a subdomain in Python? ›

Steps Needed
  1. Import request module.
  2. Create a function for scanning the subdomains and pass domain name and list of subdomains as a parameter.
  3. Run for loop for each subdomain present in the list, and concatenate subdomain with protocol and domain name in the URL sequence and stored it in the variable let named as “URL”.
Jan 23, 2023

How to implement scanner in Python? ›

Creating a port scanner with Python

Open up your Kali terminal or any Linux terminal and let's go. I'm going to show you the basics of how we write a very fundamental Python script — a basic port scanner. I want you to begin by just copying the code. Follow along, and write down exactly what I'm writing in my script.

What is a subdomain in a URL? ›

A subdomain is a prefix added to a domain name to separate a section of your website. Site owners primarily use subdomains to manage extensive sections that require their own content hierarchy, such as online stores, blogs, job boards or support platforms. Subdomains function as a separate website from its domain.

Are subdomains free? ›

Are Subdomains Free? You don't have to pay for subdomains if you already have a domain name. However, you may have to pay extra for a hosting plan that includes subdomains.

Can I create my own subdomain? ›

A subdomain is a type of DNS record that adds a prefix to your domain, such as blog.mycoolnewbusiness.com. You can create a subdomain that uses an IP address by adding an A record to your DNS zone file. If you need a subdomain that connects to another domain name, you'll need to add a CNAME record instead.

How do you create a sub list in Python? ›

A slice, or sub-list of Python list elements can be selected from a list using a colon-separated starting and ending point. The syntax pattern is myList[START_NUMBER:END_NUMBER] . The slice will include the START_NUMBER index, and everything until but excluding the END_NUMBER item.

How to create a scanner? ›

Create a Scanner Object in Java

Once we import the package, here is how we can create Scanner objects. // read input from the input stream Scanner sc1 = new Scanner(InputStream input); // read input from files Scanner sc2 = new Scanner(File file); // read input from a string Scanner sc3 = new Scanner(String str);

Is there a scanner in Python? ›

Scan in Python is a powerful tool for unit testing. It allows you to verify that a code is working correctly, identify potential problems and ensure that the code is operating as designed.

How to create a document scanner using Python? ›

How to Build the Python Document Scanner Extension
  1. Create a source distribution: python setup.py sdist.
  2. setuptools: python setup_setuptools.py build python setup_setuptools.py develop.
  3. Build wheel: pip wheel . --verbose # Or python setup.py bdist_wheel.

What is an example of a sub subdomain? ›

While a subdomain will appear before your TLD, a subdirectory link will include the subdirectory name after the original TLD. For example: Subdomain example: mysubdomain.mywebsite.com. Subdirectory example: mywebsite.com/mysubdirectory.

How to write a subdomain? ›

A subdomain is a prefix added to your original domain name, like a domain name within a domain name, usually following subdomain.example.com. They behave very similarly to an addon domain and are often created to separate your website's different sections, such as blog.example.com or store.example.com.

What is a valid subdomain example? ›

An example of a subdomain is store.yourwebsite.com . Here, 'store' is the subdomain, 'yourwebsite' is the primary domain, and '.com' is the top level domain (TLD).

What are the disadvantages of subdomains? ›

Cons of Subdomains

This can result in your SEO efforts, link juice, and growing authority profile getting diluted instead of working together to contribute to your main site and all of its pages and content as one.

Why do people use subdomains? ›

To test a new website

This is the most common reason why users create subdomains. Website developers might create subdomains as a testing or staging version of a website before it goes live.

What is the best use of subdomains? ›

You Want to Implement Distinct Design and Functionality

If a part of your website requires a very different design and functionality from your main site, a subdomain can provide the necessary distinction. For example, you might use a subdomain to distinguish the ecommerce part of your website from the main domain.

How do you create a sub class in Python? ›

In Python, you can create a subclass from a superclass using the class keyword, and by specifying the superclass in parentheses after the subclass name. The subclass inherits attributes and behaviors from the superclass, allowing you to extend or modify its functionality while reusing the existing code.

How do you write a subdomain? ›

A subdomain is a prefix added to your original domain name, like a domain name within a domain name, usually following subdomain.example.com. They behave very similarly to an addon domain and are often created to separate your website's different sections, such as blog.example.com or store.example.com.

How do you create a subdirectory in Python? ›

To safely create a nested directory in Python, you can use the os. makedirs() function from the os module. This function takes a single argument, which is the name of the directory to create, and creates all of the intermediate directories in the path if they do not already exist.

How do you create a sub dataset in Python? ›

Subset a Dataframe using Python . loc()
  1. Selecting Rows with loc() To select a single row using . loc() use the following line of code. ...
  2. Selecting rows and columns. To select specific rows and specific columns out of the data frame, use the following line of code : housing.loc[1:7,['population', 'households']]
Oct 7, 2020

References

Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 6389

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.