DEV Community

Cover image for Building a CLI Tool With Python That Solves a Real Problem
Sudhir Bahadure
Sudhir Bahadure

Posted on

Building a CLI Tool With Python That Solves a Real Problem

Introduction

According to a recent report, cyber attacks have increased by 31% in the last year, with phishing being the most common type of attack, accounting for 32% of all breaches. In this tutorial, you will learn how to build a CLI tool with Python that helps you identify potential phishing websites by checking the URL against a database of known phishing sites. To follow along, you should have basic knowledge of Python, a Python environment set up on your system, and the pip package installer.

Table of Contents

  1. Introduction
  2. Setup and Background
  3. Step 1: Setting up the Project Structure
  4. Step 2: Creating the Phishing URL Checker Function
  5. Step 3: Building the CLI Interface
  6. Real-World Application
  7. Conclusion

Setup and Background

python automation
Before we dive into the code, let's understand why this project is important. Phishing attacks can be devastating, resulting in financial loss and compromised personal data. By building a tool that can help identify potential phishing websites, we can take a proactive step in protecting ourselves and our users. We will be using the requests library to make HTTP requests and the json library to parse JSON data. Here's an example of how we can use these libraries to fetch data from a URL:

import requests
import json

def fetch_data(url):
    try:
        response = requests.get(url)
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")
        return None

# Example usage:
url = "https://example.com/data.json"
data = fetch_data(url)
print(json.dumps(data, indent=4))
Enter fullscreen mode Exit fullscreen mode

Step 1: Setting up the Project Structure

To start, create a new directory for your project and navigate to it in your terminal. Create a new file called phishing_checker.py and add the following code to set up the project structure:

mkdir phishing_checker
cd phishing_checker
touch phishing_checker.py
Enter fullscreen mode Exit fullscreen mode
import os

def create_project_structure():
    # Create directories
    dirs = ["data", "src"]
    for dir in dirs:
        if not os.path.exists(dir):
            os.makedirs(dir)

create_project_structure()
Enter fullscreen mode Exit fullscreen mode

Step 2: Creating the Phishing URL Checker Function

Next, we need to create a function that takes a URL as input and checks it against a database of known phishing sites. We will be using the phishtank API to fetch the list of phishing sites. Add the following code to the phishing_checker.py file:

import requests

def check_phishing_url(url):
    api_url = "http://checkurl.phishtank.com/checkurl/"
    params = {"url": url}
    try:
        response = requests.post(api_url, params=params)
        if response.status_code == 200:
            return response.text
        else:
            return None
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")
        return None

# Example usage:
url = "http://example.com"
result = check_phishing_url(url)
print(result)
Enter fullscreen mode Exit fullscreen mode

Step 3: Building the CLI Interface

Now that we have the phishing URL checker function, let's build a CLI interface using the argparse library. Add the following code to the phishing_checker.py file:

import argparse

def main():
    parser = argparse.ArgumentParser(description="Phishing URL Checker")
    parser.add_argument("-u", "--url", help="URL to check")
    args = parser.parse_args()
    if args.url:
        result = check_phishing_url(args.url)
        if result:
            print(f"URL {args.url} is potentially phishing")
        else:
            print(f"URL {args.url} is not phishing")
    else:
        parser.print_help()

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

You can run the CLI tool using the following command:

python phishing_checker.py -u http://example.com
Enter fullscreen mode Exit fullscreen mode

Real-World Application

This CLI tool can be used to identify potential phishing websites, helping to protect users from cyber attacks. By integrating this tool with other security tools, such as NordVPN (68% off + 3 months free), you can add an extra layer of security to your online activities. Additionally, using a reliable web hosting service like Hostinger (up to 80% off hosting) and registering your domain with a trusted registrar like Namecheap (cheapest domains online) can help prevent phishing attacks.

Conclusion

In this tutorial, you learned how to build a CLI tool with Python that helps identify potential phishing websites. The three main takeaways from this project are:

  • How to use the requests library to make HTTP requests and fetch data from a URL
  • How to use the argparse library to build a CLI interface
  • How to integrate security tools to protect users from cyber attacks To further develop your skills in Python automation, check out the next tutorial in the Python Automation Mastery series, where you will learn how to automate tasks using Python scripts. ---

💡 Found this helpful?

If this tutorial saved you time or solved a problem, consider:

Support me on Ko-fi

Every coffee keeps me writing free tutorials like this one!


This article was written with AI assistance and reviewed for technical accuracy.
Part of the **Python Automation Mastery* series — Follow for more free tutorials*

#aBotWroteThis

Top comments (0)