Skip to content

URL Expander

Abstract

Build a URL Expander application that expands shortened URLs to their original form. The app provides a GUI for entering URLs and displaying the expanded result. This project demonstrates HTTP requests, URL handling, and GUI development in Python.

Prerequisites

  • Python 3.6 or above
  • Text Editor or IDE
  • Basic understanding of Python syntax
  • Familiarity with Tkinter for GUI development
  • Knowledge of HTTP requests

Getting Started

Creating a new project

  1. Create a new project folder and name it url_expanderurl_expander.
  2. Create a new file inside the folder and name it url_expander.pyurl_expander.py.
  3. Open the project folder in your favorite text editor or IDE.
  4. Copy the code below and paste it into the url_expander.pyurl_expander.py file.

Write the code

⚙️ url_expander.py
url_expander.py
"""
URL Expander
 
A Python application that expands shortened URLs to their original form.
Features include:
- Accepting a shortened URL as input.
- Displaying the expanded URL.
"""
 
import requests
from tkinter import Tk, Label, Entry, Button, messagebox
 
 
def expand_url(short_url):
    """Expand a shortened URL to its original form."""
    try:
        response = requests.head(short_url, allow_redirects=True)
        return response.url
    except requests.RequestException as e:
        return str(e)
 
 
class URLExpanderApp:
    def __init__(self, root):
        self.root = root
        self.root.title("URL Expander")
 
        Label(root, text="Enter Shortened URL:").grid(row=0, column=0, padx=10, pady=10)
        self.url_entry = Entry(root, width=50)
        self.url_entry.grid(row=0, column=1, padx=10, pady=10)
 
        Button(root, text="Expand URL", command=self.expand_url).grid(row=1, column=0, columnspan=2, pady=10)
 
    def expand_url(self):
        """Handle the button click to expand the URL."""
        short_url = self.url_entry.get()
        if not short_url:
            messagebox.showerror("Error", "Please enter a URL.")
            return
 
        expanded_url = expand_url(short_url)
        messagebox.showinfo("Expanded URL", f"Original URL: {expanded_url}")
 
 
def main():
    root = Tk()
    app = URLExpanderApp(root)
    root.mainloop()
 
 
if __name__ == "__main__":
    main()
 
url_expander.py
"""
URL Expander
 
A Python application that expands shortened URLs to their original form.
Features include:
- Accepting a shortened URL as input.
- Displaying the expanded URL.
"""
 
import requests
from tkinter import Tk, Label, Entry, Button, messagebox
 
 
def expand_url(short_url):
    """Expand a shortened URL to its original form."""
    try:
        response = requests.head(short_url, allow_redirects=True)
        return response.url
    except requests.RequestException as e:
        return str(e)
 
 
class URLExpanderApp:
    def __init__(self, root):
        self.root = root
        self.root.title("URL Expander")
 
        Label(root, text="Enter Shortened URL:").grid(row=0, column=0, padx=10, pady=10)
        self.url_entry = Entry(root, width=50)
        self.url_entry.grid(row=0, column=1, padx=10, pady=10)
 
        Button(root, text="Expand URL", command=self.expand_url).grid(row=1, column=0, columnspan=2, pady=10)
 
    def expand_url(self):
        """Handle the button click to expand the URL."""
        short_url = self.url_entry.get()
        if not short_url:
            messagebox.showerror("Error", "Please enter a URL.")
            return
 
        expanded_url = expand_url(short_url)
        messagebox.showinfo("Expanded URL", f"Original URL: {expanded_url}")
 
 
def main():
    root = Tk()
    app = URLExpanderApp(root)
    root.mainloop()
 
 
if __name__ == "__main__":
    main()
 

Key Features

  • Accept shortened URLs as input
  • Display expanded URLs
  • GUI interface for user interaction

Explanation

Expanding URLs

The app uses the requestsrequests library to follow redirects and expand URLs:

url_expander.py
response = requests.head(short_url, allow_redirects=True)
return response.url
url_expander.py
response = requests.head(short_url, allow_redirects=True)
return response.url

Displaying Results

Expanded URLs are shown in a Tkinter message box:

url_expander.py
messagebox.showinfo("Expanded URL", f"Original URL: {expanded_url}")
url_expander.py
messagebox.showinfo("Expanded URL", f"Original URL: {expanded_url}")

Running the Application

  1. Save the file.
  2. Install required dependencies:
pip install requests
pip install requests
  1. Run the application:
python url_expander.py
python url_expander.py

Conclusion

This URL Expander project is a great way to learn about HTTP requests and URL handling in Python. You can extend it by adding batch expansion, URL validation, or analytics features.

Was this page helpful?

Let us know how we did